Rock Paper Scissors Prediction

Vishal Bakshi
08/05/2020

Training: resnet34

In [1]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline

from fastai.vision import *
from fastai.metrics import error_rate

bs = 64
In [2]:
path = Path("images/rps")
np.random.seed(42)

data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
        ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)

data.classes, data.c, len(data.train_ds), len(data.valid_ds)
Out[2]:
(['paper', 'rock', 'scissors'], 3, 1763, 440)
In [3]:
data.show_batch(rows=3, figsize=(7,6))
In [4]:
learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(4, max_lr=slice(1e-6,1e-3))
epoch train_loss valid_loss error_rate time
0 0.842282 0.048043 0.025000 00:09
1 0.395941 0.016161 0.004545 00:08
2 0.247190 0.016520 0.006818 00:09
3 0.163695 0.015629 0.004545 00:08
In [5]:
interp = ClassificationInterpretation.from_learner(learn)
losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs)
Out[5]:
True
In [11]:
interp.plot_confusion_matrix(dpi=90)

Predictions

I took 15 pictures of rock, paper and scissors hand gestures and will use this model to predict them:

In [30]:
open_image(path/"prediction"/"rock"/"01.png")
Out[30]:
In [31]:
open_image(path/"prediction"/"paper"/"01.png")
Out[31]:
In [32]:
open_image(path/"prediction"/"scissors"/"01.png")
Out[32]:
In [33]:
pred_class,pred_idx,outputs = learn.predict(open_image(path/"prediction"/"rock"/"01.png"))
pred_class.obj
Out[33]:
'rock'
In [34]:
pred_class,pred_idx,outputs = learn.predict(open_image(path/"prediction"/"paper"/"01.png"))
pred_class.obj
Out[34]:
'paper'
In [35]:
pred_class,pred_idx,outputs = learn.predict(open_image(path/"prediction"/"scissors"/"01.png"))
pred_class.obj
Out[35]:
'scissors'

Hooray! Running through all 15 images...

In [22]:
classes = ['rock', 'paper', 'scissors']
pred_results = {'rock': [], 'paper': [], 'scissors': []}
for item in classes:
    for i in range(5):
        filename = f'{i+1:02}.png'
        img = open_image(path/"prediction"/item/filename)
        result,_,_ = learn.predict(img)
        pred_results[item].append(result.obj)
In [23]:
pred_results
Out[23]:
{'rock': ['rock', 'rock', 'rock', 'rock', 'rock'],
 'paper': ['paper', 'paper', 'paper', 'paper', 'paper'],
 'scissors': ['scissors', 'scissors', 'scissors', 'scissors', 'scissors']}

Amazing! All 15 images were predicted correctly.