<jupyter_start><jupyter_text>Generate model with VGG16 feature extractors, set trainable false<jupyter_code>temperature=2

def softmaxTemp(x):
    return K.softmax(x/temperature)

def getModel( output_dim):
    # output_dim: the number of classes (int)
    # return: compiled model (keras.engine.training.Model)
    
    vgg_model = VGG16( weights='imagenet', include_top=True )
    vgg_out = vgg_model.layers[-1].output
    
    out = Dense( output_dim, activation='softmax')( vgg_out )
    
    tl_model = Model( input=vgg_model.input, output=out)
    
    tl_model.layers[-2].activation=softmaxTemp
    
    for layer in tl_model.layers[0:-1]:
        layer.trainable = False            

    tl_model.compile(loss= "categorical_crossentropy", optimizer="adagrad", metrics=["acc"])
    tl_model.summary()
    return tl_model<jupyter_output><empty_output><jupyter_text>Loading all images into 'album'<jupyter_code>def loadImages(path = '/mnt/cube/UT_/Urban_tribes/'):
    album = {}
    for item in listdir(path):
        category = item[0:4]
        if category == ".ipy":
            continue
        if category not in album:
            album[category] = []
        
        img = load_img(path+item)
        img = img_to_array(img)
        img = misc.imresize(img, (224, 224))
        img = scipy.misc.imrotate(img, 180)
        album[category].append(img)
    return album
album = loadImages()
# plt.imshow(album['goth'][0][:, :, 0])
# plt.show()<jupyter_output><empty_output><jupyter_text>Split 'album' into datasetAlbum is split into training and testing input/outputs according to number of examples attribute<jupyter_code>def make_dataset(album, numExamples):
    trn_inp = []
    trn_out = []
    tst_inp = []
    tst_out = []
    keys = album.keys()
    for key in keys:
        examples = album[key]
        l = len(examples)
        idx = np.random.choice(l, numExamples+30)
        for i in idx[:-5]:
            trn_inp.append(examples[i])
            trn_out.append(keys.index(key))
        for i in idx[-5:]:
            tst_inp.append(examples[i])
            tst_out.append(keys.index(key))
    return [trn_inp, trn_out, tst_inp, tst_out]      
# plt.imshow(album['bike'][0][:, :, 0])
# # plt.show()
# album.keys()
dataset = make_dataset(album, 70)
trainX = np.array(dataset[0])
trainY = np.array(dataset[1])

# Now shuffle the training data and swapaxes
idx = np.random.choice(len(trainX), len(trainX))
trainX = trainX[idx]
trainY = trainY[idx]

trainX = preprocess_input(np.float64(trainX)).swapaxes(1, 3).swapaxes(2, 3)
trainY = np_utils.to_categorical(trainY)

# Only swapaxes for testing data
testX = np.array(dataset[2])
testY = np.array(dataset[3])

testX = preprocess_input(np.float64(testX)).swapaxes(1, 3).swapaxes(2, 3)
testY = np_utils.to_categorical(testY)
# plt.imshow(trainX[10][0, :, :])
# plt.show()<jupyter_output><empty_output><jupyter_text>Fit the model<jupyter_code>model = getModel(11)
history = model.fit(trainX, trainY, batch_size = 16, nb_epoch = 30, validation_data = (testX, testY), shuffle=True)<jupyter_output>Train on 1045 samples, validate on 55 samples
Epoch 1/30
{'fraction_non_alphanumeric': 0.1347449470644851, 'fraction_numerical': 0.02053256336220725, 'mean_word_length': 3.6056129985228953, 'pattern_counts': {'":': 1, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '371738', 'license': 'MIT', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1124, 'n_words': 220, 'path': '.ipynb_checkpoints/Urban Tribes-Dark Knowledge-checkpoint.ipynb', 'repo_name': 'aliasvishnu/Keras-VGG16-TransferLearning', 'size': '3117'}