def predict_from_model(image): image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) trn_tfms, val_tfms = tfms_from_model(resnext29_8_64(), IMAGE_SIZE) # get transformations # im = val_tfms(open_image('./data/test/8/758.jpg')) im = val_tfms(image) model.precompute = False # We'll pass in a raw image, not activations preds = model.predict_array(im[None]) prediction = np.argmax(preds) # preds are log probabilities of CLASSES return prediction
def init_model(): global model os.makedirs(PATH, exist_ok=True) data = get_data( BATCH_SIZE, IMAGE_SIZE) # data generator for batch size=32, image size=32x32 mod = resnext29_8_64() basemodel = BasicModel(mod.cuda(), name='cifar10_rn29_8_64') model = ConvLearner(data, basemodel) model.load("32x32_8")
files = os.listdir(f'{PATH}valid/cats')[:5] img = plt.imread(f'{PATH}valid/cats/{files[0]}') ''' Model and learning rate schedule ''' ''' Data augmentation ''' sz = 112 tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1) #ims = np.stack([get_augs() for i in range(6)]) #plots(ims, rows=2) data = ImageClassifierData.from_paths(PATH, tfms=tfms) # By default when we create a learner, it sets all but the last layer to *frozen*. That means that it's still only updating the weights in the last layer when we call `fit`. from fastai.models.cifar10.resnext import resnext29_8_64 m = resnext29_8_64() bm = BasicModel(m.cuda(), name='cifar10_rn29_8_64') #arch=resnet34 #learn = ConvLearner.pretrained(arch, data, precompute=True) learn = ConvLearner(data, bm) learn.unfreeze() lrf = learn.lr_find() #learn.sched.plot() wd = 5e-4 learn.fit(lr, 1) learn.fit(lr, 2, cycle_len=1) learn.fit(lr, 3, cycle_len=1, cycle_mult=2, wds=wd) #learn.sched.plot_lr() ''' Now that final layer is trained, fine-tuning the other layers to unfreeze the remaining layers