I need to understand what is going wrong when I use the code from the lessons for my experiments. I use the following code to train:
1 from fastai.vision.all import *
2 if __name__ == '__main__':
3 path = 'C:/training/cats/'
4 #the following line was added because of Microsoft Windows and computer
5 num_workers=0
6 silence = DataBlock(
7 blocks=(ImageBlock, CategoryBlock),
8 get_items=get_image_files,
9 splitter=RandomSplitter(valid_pct=0.2, seed=42),
10 get_y=parent_label,
11 item_tfms=Resize(224, ResizeMethod.Pad, pad_mode='zeros'))
12 dls = silence.dataloaders(path)
13 learn = cnn_learner(dls, resnet18,
14 model_dir='C:/Users/jbiss/workspace/AI/VoiceRecognition/models/',
15 metrics=error_rate)
16 learn.fine_tune(4)
17 dls.valid.show_batch(max_n=4, nrows=1)
18 dls.train.show_batch(max_n=4, nrows=1)
19 learn.export()
- Line 3: trains on Abyssinian cats from oxford-iiit-pet downloaded in the class
- Lines 6-11: my DataBlock copied from the 02_production.ipynb file
- Line 11: alternate transform due to some problem that the process had when working with B&W images that were all resized to 224x224
Training results in the following:
epoch train_loss valid_loss error_rate time
0 0.000000 0.000000 0.000000 00:39
epoch train_loss valid_loss error_rate time
0 0.000000 0.000000 0.000000 00:36
1 0.000000 0.000000 0.000000 00:37
2 0.000000 0.000000 0.000000 00:37
3 0.000000 0.000000 0.000000 00:37
I test that model using the following image:
with the following code:
from fastai.vision.all import *
from PIL import Image
import os
print('__name__ is: ', __name__)
if __name__ == '__main__':
learn_inf = load_learner('C:/models/export.pkl')
pred,pred_idx,probs = learn_inf.predict('C:/spectrum_files/spec_1.png')
print('pred is: ', pred, ', pred_idx is: ', pred_idx, ' and probs is: ', probs)
else: print('cannot do it')
and get the following results:
pred is: cats , pred_idx is: tensor(0) and probs is: tensor([1.])
So, what is going on in my Notebook that results in a totally incorrect prediction but a correct prediction in the course?