<jupyter_start><jupyter_text>We load a VGG pretrained model and only select the first 23 layers.<jupyter_code>vgg16 = models.vgg16(pretrained=True)
newmodel = torch.nn.Sequential(*(list(vgg16.features[:24])))<jupyter_output><empty_output><jupyter_text>We set up the dataloaders and their augmentations here<jupyter_code>transform = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.RandomHorizontalFlip(),
    #transforms.RandomVerticalFlip(),
    transforms.RandomRotation((-20,20)),
    transforms.ToTensor(),
    transforms.Normalize(mean = [ 0.485, 0.456, 0.406 ], #default
    std  = [ 0.229, 0.224, 0.225 ]) # default
    #transforms.Normalize(mean = [ 0.71, 0.67, 0.59 ],
    #std  = [ 0.064, 0.059, 0.05]),
    
    ])

transform2 = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean = [ 0.485, 0.456, 0.406 ], #default
    std  = [ 0.229, 0.224, 0.225 ]) # default
    #transforms.Normalize(mean = [ 0.71, 0.67, 0.59 ],
    #std  = [ 0.064, 0.059, 0.05]),
    
    ])


# transform = transforms.Compose([
#     transforms.Resize(224),
#     transforms.ToTensor(),
#     transforms.Normalize(mean = [ 0.485, 0.456, 0.406 ], #default
#     std  = [ 0.229, 0.224, 0.225 ]), # default
#     #transforms.Normalize(mean = [ 0.71, 0.67, 0.59 ],
#     #std  = [ 0.064, 0.059, 0.05]),
    
#     ])

trainData = datasets.ImageFolder('../data/processed/classifier_training', transform)
trainLoader = torch.utils.data.DataLoader(dataset=trainData, batch_size=1, shuffle=True)<jupyter_output><empty_output><jupyter_text>Here we extract vector encodings for drawings and photos from the VGG model.<jupyter_code>drawing_encodings = []
photo_encodings = []
for images, labels in trainLoader:
    images = Variable(images)
    labels = Variable(labels)
    encoding = newmodel(images)
    encoding = encoding.detach().numpy().flatten()
    if labels == 0:
        drawing_encodings.append(encoding)
    else:
        photo_encodings.append(encoding)
        
photos = np.asarray(photo_encodings)
drawings = np.asarray(drawing_encodings)<jupyter_output><empty_output><jupyter_text>Next we use these encodings to train a Linear SVC classifier<jupyter_code>clf = svm.LinearSVC(random_state=0)
X = np.concatenate((drawings, photos),axis=0)
y = np.concatenate((np.zeros(len(drawing_encodings)), np.ones(len(photo_encodings))),axis=0)

# TODO SAVE CLASSIFIER
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.10, random_state=42)
clf.fit(X_train, y_train)
outscore = cross_val_score(clf,X,y, cv=10)
print('our accuracy is estimated at: ' + str(outscore)) #layer 24
np.round(np.mean(outscore), 2)
np.round(np.std(outscore), 2)
filename = '../data/processed/finalized_photo_illustration_model.sav'
pickle.dump(clf, open(filename, 'wb'))<jupyter_output><empty_output><jupyter_text>Predicting type of imageHere we predict which type of image (photo or drawing) we are dealing with. The images are then placed in folders belonging to this type.<jupyter_code>predictData = ImageFolderWithPaths('../data/processed/gender_/', transform2)
predictLoader = torch.utils.data.DataLoader(dataset=predictData, batch_size=1, shuffle=False)
# check if dir exists
for dir_ in ['test', 'train', 'validation']:
    os.mkdir('../data/processed/gender_/' + dir_ + '/f_photo')
    os.mkdir('../data/processed/gender_/' + dir_ + '/m_photo')
    os.mkdir('../data/processed/gender_/' + dir_ + '/f_drawing')
    os.mkdir('../data/processed/gender_/' + dir_ + '/m_drawing')
for images, labels, paths in predictLoader:
    path = ' '.join(paths)
    path_ = path.split('/')
    images = Variable(images)
    encoding = newmodel(images)
    encoding = encoding.detach().numpy().flatten()
    y_predict = clf.predict((np.asarray(encoding).reshape(1,-1)))
    if y_predict == 0:
        newpath ='../data/processed/gender_/' + path_[4] + '/' + path_[5] + '_drawing/' + path_[6]
        shutil.copy(path, newpath)
    else:
        newpath = '../data/processed/gender_/' + path_[4] + '/' + path_[5] + '_photo/' + path_[6]
        shutil.copy(path, newpath)
        
img_path = '../data/processed/KB_faces/1978/KBNRC01-000026215-mpeg21-a0080.jpg'

preprocess_img = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225])
    ])

vgg16 = models.vgg16(pretrained=True)
newmodel = torch.nn.Sequential(*(list(vgg16.features[:24])))

filename = "finalized_photo_illustration_model.sav.sav"
clf = pickle.load(open(filename, 'rb'))
img = Image.open(img_path)
img_tensor = preprocess_img(img)
img_tensor.unsqueeze_(0)
images = Variable(img_tensor)
encoding = newmodel(images)
encoding = encoding.detach().numpy().flatten()
prediction = clf.predict((np.asarray(encoding).reshape(1,-1)))
prediction<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15616218386190284, 'fraction_numerical': 0.05038137294259334, 'mean_word_length': 4.206896551724138, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '448888', 'license': 'MIT', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1766, 'n_words': 316, 'path': 'notebooks/3-mw-separatephoto.ipynb', 'repo_name': 'melvinwevers/detecting_faces-medium-types-gender', 'size': '4982'}