def transform_predict(data, data_shape=(3, 331, 331), resize=331): ctx = data.context im = resize_longer(data.asnumpy(), resize, data_shape) im = nd.array(im, ctx=ctx) im = im.astype('float32') / 255 auglist = [ # image.RandomGrayAug(1.0), image.ColorNormalizeAug(mean=(0.417, 0.402, 0.366), std=(0.081, 0.079, 0.080)), ] for aug in auglist: im = aug(im) im = nd.transpose(im, (2, 0, 1)) return nd.stack(im)
def transform_val(data, label, data_shape=(3, 331, 331), resize=331): # im = data.astype('float32') / 255, ctx = data.context im = resize_longer(data.asnumpy(), resize, data_shape) im = nd.array(im, ctx=ctx) im = im.astype('float32') / 255 auglist = [ # image.RandomGrayAug(1.0), # image.ColorNormalizeAug(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), image.ColorNormalizeAug(mean=(0.417, 0.402, 0.366), std=(0.081, 0.079, 0.080)), ] for aug in auglist: im = aug(im) # im = im.clip(0, 1) im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label], ctx=ctx).asscalar()
def transform_train_DA3(data, label): im = data.astype(np.float32) / 255 auglist = [image.RandomSizedCropAug(size=(32, 32), min_area=0.49, ratio=(0.5, 2))] _aug = image.CreateAugmenter(data_shape=(3, 32, 32), resize=0, rand_crop=False, rand_resize=False, rand_mirror=True, # mean=np.array([0.4914, 0.4822, 0.4465]), # std=np.array([0.2023, 0.1994, 0.2010]), brightness=0.3, contrast=0.3, saturation=0.3, hue=0.3, pca_noise=0.01, rand_gray=0, inter_method=2) auglist.append(image.RandomOrderAug(_aug)) for aug in auglist: im = aug(im) if random.random() > random_clip_rate: im = im.clip(0, 1) _aug = image.ColorNormalizeAug(mean=np.array([0.4914, 0.4822, 0.4465]), std=np.array([0.2023, 0.1994, 0.2010]), ) im = _aug(im) im = nd.transpose(im, (2, 0, 1)) return (im, nd.array([label]).asscalar().astype('float32'))
def transform_train(data, label, data_shape=(3, 363, 363), resize=363): # data [height, width, channel] # im = data.astype('float32') / 255, ctx = data.context im = resize_longer(data.asnumpy(), resize, data_shape) im = rotate_image(im) im = nd.array(im, ctx=ctx) im = im.astype('float32') / 255 auglist = [ image.RandomCropAug((331, 331)), image.ColorJitterAug(0.3, 0.3, 0.3), image.RandomGrayAug(0.5), # image.ColorNormalizeAug(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)), image.ColorNormalizeAug(mean=(0.417, 0.402, 0.366), std=(0.081, 0.079, 0.080)), ] for aug in auglist: im = aug(im) # im = im.clip(0, 1) im = nd.transpose(im, (2, 0, 1)) return im, nd.array([label], ctx=ctx).asscalar()
import gc from tqdm import tqdm from sklearn.model_selection import train_test_split # In[ ]: # try run via gpu ctx = mx.gpu() # In[ ]: transformers = [ # 强制resize成pretrain模型的输入大小 image.ForceResizeAug((224, 224)), # 标准化处理 image.ColorNormalizeAug(mean=nd.array([0.485, 0.456, 0.406]), std=nd.array([0.229, 0.224, 0.225])) ] # Transoform image and label to our target format data # # 为避免过拟合,我们在这里使用`image.CreateAugmenter`来加强数据集。例如我们设`rand_mirror=True`即可随机对每张图片做镜面反转。我们也通过`mean`和`std`对彩色图像RGB三个通道分别做标准化。以下我们列举了该函数里的所有参数,这些参数都是可以调的。 # In[ ]: train_augs_params = { "resize": 1, "rand_crop": False, "rand_resize": False, "rand_mirror": True, "mean": np.array([0.4914, 0.4822, 0.4465]), "std": np.array([0.2023, 0.1994, 0.2010]),