|
|
from fastbook import * |
|
|
from fastai.vision.all import * |
|
|
from fastai.vision.widgets import * |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
path = Path('alien_or_not') |
|
|
|
|
|
|
|
|
|
|
|
aliens = DataBlock( |
|
|
blocks=(ImageBlock, CategoryBlock), |
|
|
get_items=get_image_files, |
|
|
splitter=RandomSplitter(valid_pct=0.2, seed=42), |
|
|
get_y=parent_label, |
|
|
item_tfms=Resize(128)) |
|
|
|
|
|
dls = aliens.dataloaders(path) |
|
|
dls.valid.show_batch(max_n=4, nrows=1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
learn = vision_learner(dls, resnet50, metrics=error_rate) |
|
|
learn.fine_tune(500) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
is_alien, _, probs = learn.predict(PILImage.create('test_images/4.jpg')) |
|
|
|
|
|
print(f"This is a: {is_alien}.") |
|
|
print(f"Probability it's an alien: {probs[0]:.4f}") |
|
|
|
|
|
|
|
|
|
|
|
learn.export('model.pkl') |