<jupyter_start><jupyter_text>Select the amount of classes that will be used<jupyter_code># pick the n classes with the most occuring instances
amt = 5
classes = data.top_classes(dataset.labels, amt)
classes
def extract_topx_classes(classes, train_or_test):
    name_list = []
    n_per_class = []
    tail = '.jpg'
    for cls in classes:
        print(cls)
        names = data.items_with_label(dataset.labels, cls)
        if train_or_test == 'train':
            train_names = [f for f in names if (f) in dataset.train]
        if train_or_test == 'test':
            train_names = [f for f in names if (f) in dataset.test]
        if train_or_test == 'validation':
            train_names = [f for f in names if (f) in dataset.validation]
        name_list.append(train_names)
        n_per_class.append(len(train_names))

    n = min(n_per_class)
    # (optional) reduce n to check whether the model can rember its input
#     reduced_n = 50
#     if n > reduced_n:    n = reduced_n
    x = []
    for ls in name_list:
        for name in ls:
            x.append(name)
    random.shuffle(x)
    return x

x_train = extract_topx_classes(classes, 'train')
x_test = extract_topx_classes(classes, 'test')
x_validation = extract_topx_classes(classes, 'validation')

print(x_train[0:10])<jupyter_output><empty_output><jupyter_text>Load and convert images Training data<jupyter_code>x_train, y_train, n = data.extract_all(dataset, x_train)
# y_test = y_train

print(y1_train)
plt.imshow(x_train[0])<jupyter_output><empty_output><jupyter_text>Testing data<jupyter_code>x_test, y_test, n = data.extract_all_test(dataset, x_test)<jupyter_output>('extract all data:', 300)<jupyter_text>Validation data<jupyter_code>x_validation, y_validation, n  = data.extract_all_validation(dataset, x_validation)<jupyter_output>('extract all data:', 150)<jupyter_text>Convert labels and determine input and output shape for model<jupyter_code>imp.reload(data)
# y_train, y_test, y_validation = data.labels_to_vectors(dataset, y_train, y_test, y_validation)
y_train = data.one_hot(y_train)
y_test = data.one_hot(y_test)
y_validation = data.one_hot(y_validation)
input_shape = x_train.shape[1:] # = shape of an individual image (matrix)
output_length = (y_train[0]).shape[0] # = length of an individual label

print(x1_train[0:10])
print(y_train[0:10])<jupyter_output><empty_output><jupyter_text>Initiate VG16 network and add extra layers<jupyter_code>from keras.models import Model 

def get_model(learn_rate=0.0001, batches=10, dropout=0.10):
    # build the VGG16 network
    model = applications.VGG16(weights='imagenet', include_top=False, input_shape = input_shape )
    print('Model loaded.')

    # build a classifier model to put on top of the convolutional model
    # top_model = Sequential()
    # top_model.add(Flatten(input_shape=model.output_shape[1:]))
    # top_model.add(Dense(256, activation='relu'))
    # top_model.add(Dropout(0.5))
    # top_model.add(Dense(1, activation='sigmoid'))

    for layer in model.layers[:13]:
        layer.trainable = False

    #Adding custom Layers 
    x = model.output
    x = Flatten()(x)
    x = (Dropout(dropout))(x)
    x = (Dense(2048, activation='relu'))(x) #128
    x = (Dense(2048, activation='relu'))(x) #128
    x = (Dense(1024, activation='relu'))(x)
    # softmax output to get probability distribution
    predictions = Dense(output_length, activation="softmax")(x) #activation="softmax"


    # creating the final model 
    model_final = Model(input = model.input, output = predictions)

    # Adam, SGD
    # sgd = Keras.optimizers.SGD(lr=0.01, clipnorm=1.)
    optimizer = optimizers.Adam(lr=learn_rate)

    # compile the model 
    model_final.compile(loss = "categorical_crossentropy", optimizer = optimizer, metrics=['accuracy',
        'mean_squared_error','categorical_crossentropy','top_k_categorical_accuracy'])
    
    return model_final<jupyter_output><empty_output><jupyter_text>Hyperparameter selection<jupyter_code>from sklearn.grid_search import RandomizedSearchCV
from keras.wrappers.scikit_learn import KerasClassifier
import time

# fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)

batches = [5, 10, 20]
learn_rate = [0.0001, 0.001, 0.01]
dropout = [0.1, 0.3, 0.5] # most used values
param_grid = dict(learn_rate=learn_rate, batch_size=batches, dropout=dropout)

# tune the hyperparameters via a randomized search
model = KerasClassifier(build_fn=get_model, verbose=0)
grid = RandomizedSearchCV(estimator=model, param_distributions=param_grid)
start = time.time()
grid.fit(x_validation, y_validation)
 
# evaluate the best randomized searched model on the testing
# data
print("[INFO] randomized search took {:.2f} seconds".format(
    time.time() - start))
acc = grid.score(x_validation, y_validation)
print("[INFO] grid search accuracy: {:.2f}%".format(acc * 100))
print("[INFO] randomized search best parameters: {}".format(
    grid.best_params_))<jupyter_output><empty_output><jupyter_text>data augmentation<jupyter_code>#Initiate the train and test generators with data Augumentation 
train_datagen = ImageDataGenerator(
    rescale = 1./255,
    horizontal_flip = True,
    fill_mode = "nearest",
    zoom_range = 0.3,
    width_shift_range = 0.3,
    height_shift_range = 0.3,
    rotation_range = 30)

validate_datagen = ImageDataGenerator(
    rescale = 1./255,
    horizontal_flip = True,
    fill_mode = "nearest",
    zoom_range = 0.3,
    width_shift_range = 0.3,
    height_shift_range = 0.3,
    rotation_range = 30)<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code># n epochs = n iterations over all the training data
batch_size = 10
epochs = 15<jupyter_output><empty_output><jupyter_text>With data augmentation<jupyter_code>model_final_augmentation = get_model()

train_datagen.fit(x_train)
val = validate_datagen.fit(x_validation)
model_final_augmentation.fit_generator(train_datagen.flow(x_train, y_train, batch_size=batch_size), 
                                                           epochs=epochs, 
                                                           callbacks=[TensorBoard(log_dir=config.tmp_model_dir)])<jupyter_output>Model loaded.<jupyter_text>Without data augmentation<jupyter_code>model_final = get_model()

from keras.callbacks import TensorBoard
model_final.fit(x_train, y_train, epochs=epochs, batch_size=batch_size,
          validation_split=1/6, callbacks=[TensorBoard(log_dir=config.tmp_model_dir)])

model.save(config.dataset_dir + 'models/')<jupyter_output>Model loaded.<jupyter_text>save the model<jupyter_code>from keras.models import model_from_json

def save_model(model, model_name, weight_name):
    # serialize model to JSON
    model_json = model.to_json()
    with open(config.dataset_dir + 'models/' + model_name, "w") as json_file:
        json_file.write(model_json)
    # serialize weights to HDF5
    model.save_weights(config.dataset_dir + 'models/' + weight_name, "w")
    print("Saved model to disk")<jupyter_output>Saved model to disk<jupyter_text>load a model<jupyter_code># load json and create model
def load_model(model, weights)
    json_file = open(model, 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
    # load weights into new model
    loaded_model.load_weights(weights)
    print("Loaded model from disk")
    return loaded_model<jupyter_output><empty_output><jupyter_text>running tests<jupyter_code>import numpy as np
# import sklearn.metrics.confusion_matrix

def evaluate(model):
    cvscores = []
    scores = model.evaluate(x_test, y_test, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    cvscores.append(scores[1] * 100)
    print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))

evaluate(model_final_augmentation)<jupyter_output>acc: 54.67%
54.67% (+/- 0.00%)<jupyter_text>load models<jupyter_code>model_augment = config.dataset_dir + 'models/cnntransfer_augm.json'
model_augment_weights = config.dataset_dir + 'models/cnntransferweights_augmen.h5'
model_default = config.dataset_dir + 'models/cnntransfer.json'
model_default_weights = config.dataset_dir + 'models/cnntransferweights.h5'

# augment = load_model(model_augment, model_augment_weights)
default = load_model(model_default, model_default_weights)
augment = load_model(model_augment, model_augment_weights)<jupyter_output><empty_output><jupyter_text>Create standard conf matrix<jupyter_code>import tensorflow as tf

y_pred_class = model_final.predict(x_test)
# con = tf.confusion_matrix(labels=y_test, predictions=y_pred_class )
# print(con)

y_test_non_category = [ np.argmax(t) for t in y_test ]
y_predict_non_category = [ np.argmax(t) for t in y_pred_class ]


from sklearn.metrics import confusion_matrix
conf_mat = confusion_matrix(y_test_non_category, y_predict_non_category)
print(conf_mat)
# def confusion_matrix(model, x_test)
#     y_pred_class = model_final.predict(X_test)
#     con = tf.confusion_matrix(labels=y_train, predictions=y_pred_class )
#     print(cont)

from mlxtend.plotting import plot_confusion_matrix
fig, ax = plot_confusion_matrix(conf_mat=conf_mat)<jupyter_output><empty_output><jupyter_text>Baseline<jupyter_code>import cv2

gray_xtrain = []
gray_xtest = []

pcanet = PCANet(
    image_shape=(224),
    filter_shape_l1=2, step_shape_l1=1, n_l1_output=4,
    filter_shape_l2=2, step_shape_l2=1, n_l2_output=4,
    block_shape=2
)

pcanet.validate_structure()

for img in x_train:
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    gray_xtrain.append(img)

for img in x_test:
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    gray_xtest.append(img)

pcanet.fit(np.array(gray_xtrain))
X_train = pcanet.transform(gray_xtrain)
X_test = pcanet.transform(gray_xtest)

model = RandomForestClassifier(n_estimators=100, random_state=1234)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("accuracy: " + str(accuracy))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11005692599620494, 'fraction_numerical': 0.018775591730750024, 'mean_word_length': 4.996407185628742, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 64, 'https://': 0, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '169986', 'license': 'MIT', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3488, 'n_words': 682, 'path': 'src/nn/transfer learning_final.ipynb', 'repo_name': 'voschezang/trash-image-classification', 'size': '10013'}