| 23 | |
| 24 | |
| 25 | def get_params(opt, size): |
| 26 | w, h = size |
| 27 | new_h = h |
| 28 | new_w = w |
| 29 | if opt.preprocess_mode == 'resize_and_crop': |
| 30 | new_h = new_w = opt.load_size |
| 31 | elif opt.preprocess_mode == 'scale_width_and_crop': |
| 32 | new_w = opt.load_size |
| 33 | new_h = opt.load_size * h // w |
| 34 | elif opt.preprocess_mode == 'scale_shortside_and_crop': |
| 35 | ss, ls = min(w, h), max(w, h) # shortside and longside |
| 36 | width_is_shorter = w == ss |
| 37 | ls = int(opt.load_size * ls / ss) |
| 38 | new_w, new_h = (ss, ls) if width_is_shorter else (ls, ss) |
| 39 | |
| 40 | x = random.randint(0, np.maximum(0, new_w - opt.crop_size)) |
| 41 | y = random.randint(0, np.maximum(0, new_h - opt.crop_size)) |
| 42 | |
| 43 | flip = random.random() > 0.5 |
| 44 | return {'crop_pos': (x, y), 'flip': flip} |
| 45 | |
| 46 | |
| 47 | def get_transform(opt, params, method=Image.BICUBIC, normalize=True, toTensor=True): |