<jupyter_start><jupyter_text>Inference on Project Sidewalk ResNet18Using the 15% Test SetUsing ROI ImagesOnly 3 classes (but we still need to state all images<jupyter_code>import numpy as np
import pandas as pd
import os

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns


import torch.nn as nn
import torch
import torch.utils.model_zoo as model_zoo

import torchvision.models as models # To get ResNet18

# From - https://github.com/cfotache/pytorch_imageclassifier/blob/master/PyTorch_Image_Inference.ipynb

from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from PIL import Image
from torch.autograd import Variable

from torch.utils.data.sampler import SubsetRandomSampler<jupyter_output><empty_output><jupyter_text>Prepare the Model<jupyter_code>SAGEMAKER_PATH = r'/home/ec2-user/SageMaker'

MODEL_PATH = os.path.join(SAGEMAKER_PATH, r'sidewalk-cv-assets19/pytorch_pretrained/models/20e_slid_win_no_feats_r18.pt')
os.path.exists(MODEL_PATH)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device
# Use PyTorch's ResNet18
# https://stackoverflow.com/questions/53612835/size-mismatch-for-fc-bias-and-fc-weight-in-pytorch
model = models.resnet18(num_classes=5)  
model.to(device)
model.load_state_dict(torch.load(MODEL_PATH))
model.eval()<jupyter_output><empty_output><jupyter_text>Prep Data<jupyter_code>!rm -f -r /home/ec2-user/SageMaker/classify-streetview/roi-images/test/.ipynb_checkpoints/
# Load in the data
SAGEMAKER_PATH = r'/home/ec2-user/SageMaker'
data_dir = os.path.join(SAGEMAKER_PATH, 'classify-streetview/roi-images/test')

test_transforms = transforms.Compose([transforms.Resize(224),
                                      transforms.ToTensor()
                                     ])

data = datasets.ImageFolder(data_dir, transform=test_transforms)
classes = data.classes
classes
df_testnames = pd.read_csv(os.path.join(SAGEMAKER_PATH, 'classify-streetview/label-crops/test_roi_image_locations_sorted.csv'))
print(df_testnames.shape)
df_testnames.head()
num = 1000
indices = list(range(len(data)))
print(indices)
np.random.shuffle(indices)
idx = indices[:num]

test_transforms = transforms.Compose([transforms.Resize(224),
                                      transforms.ToTensor(),
                                     ])

#sampler = SubsetRandomSampler(idx)
loader = torch.utils.data.DataLoader(data, batch_size=num)
dataiter = iter(loader)
images, labels = dataiter.next()
len(images)
labels
def predict_image(image, model):
    image_tensor = test_transforms(image).float()
    image_tensor = image_tensor.unsqueeze_(0)
    input = Variable(image_tensor)
    input = input.to(device)
    output = model(input)
    index = output.data.cpu().numpy().argmax()
    return index, output  
int(np.ceil(len(images) / 5))
plt.cla()
plt.clf()

predict_dict = {}
output_dict = {}

to_pil = transforms.ToPILImage()

for ii, row in df_testnames.iterrows():
    imagename = row['jpg_name']
    image = to_pil(images[ii])
    index, output = predict_image(image, model)
    actual_class = str(classes[int(labels[ii])])
    pred_class = str(classes[index])
    output_dict[imagename] = output.tolist()[0]
    predict_dict[imagename] = pred_class

df_predict = pd.DataFrame.from_dict(predict_dict, orient='index')
df_predict.columns = ['prediction']
df_predict.head()
df_output = pd.DataFrame.from_dict(output_dict, orient='index')
df_output.head()
df_all = df_predict.merge(df_output, how = 'left', left_index = True, right_index = True)
df_all = df_all.reset_index()
print(df_all.shape)
df_all.columns = ['jpg_name', 'prediction', 'pred_0_missing', 'pred_1_null', 'pred_2_obstacle', 'pred_3_missing', 'pred_4_surface_prob']
df_all.head()<jupyter_output>(958, 7)<jupyter_text>Merge CSV Results with Ground Truth<jupyter_code>df_merge = df_all.merge(df_testnames, how = 'inner', left_on = 'jpg_name', right_on = 'jpg_name')
print(df_merge.shape)
df_merge.head()
df_merge_group = df_merge.groupby(['prediction', 'ground_truth'])['jpg_name'].count()
df_merge_group
df_confuse = df_merge_group.unstack('prediction')
df_confuse['0_missing'] = 0
df_confuse = df_confuse.reindex(sorted(df_confuse.columns), axis = 1)
df_confuse = df_confuse.reindex(sorted(df_confuse.index), axis = 0)
df_confuse
# Overall Accuracy - str
(575 + 18) / df_confuse.sum().sum()
OUTPUT_DIR = r'/home/ec2-user/SageMaker/classify-streetview/model0-roi-images'
df_merge.to_csv(os.path.join(OUTPUT_DIR, '2020-04-04-InferenceNoTrain.csv'), index = False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1218872870249017, 'fraction_numerical': 0.01529051987767584, 'mean_word_length': 5.17948717948718, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '628328', 'license': 'MIT', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1628, 'n_words': 282, 'path': 'model3-pretrained-attempt-train/2020-04-04-InferenceWithNoTrain.ipynb', 'repo_name': 'w210-accessibility/classify-streetview', 'size': '4578'}