<jupyter_start><jupyter_text>Setup neural network<jupyter_code>base_model = MobileNet(weights = 'imagenet', include_top = False) # imports the mobilenet model and discards the last 1000 neuron layer.
x = base_model.output
x = GlobalAveragePooling2D()(x)
# add dense layers so that the model can learn more complex functions and classify for better results
x = Dense(1024, activation = 'relu')(x)
# dense layer 2
x = Dense(1024, activation = 'relu')(x)
# dense layer 3
x = Dense(512, activation = 'relu')(x)
# final layer with softmax activation
preds = Dense(2, activation = 'softmax')(x)<jupyter_output>WARNING:tensorflow:`input_shape` is undefined or non-square, or `rows` is not in [128, 160, 192, 224]. Weights for input shape (224, 224) will be loaded as the default.<jupyter_text>15 img for train<jupyter_code>model_15 = Model(inputs = base_model.input, outputs = preds)

for layer in model_15.layers:
    layer.trainable = False
for layer in model_15.layers[:20]:
    layer.trainable = False
for layer in model_15.layers[20:]:
    layer.trainable = True
train_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)

train_generator = train_datagen.flow_from_directory(
    'data/archive/images/15-n02090622-borzoi/', 
    target_size=(224,224),
    color_mode='rgb',
    batch_size=32,
    class_mode='categorical',
    shuffle=True)
model_15.compile(optimizer = 'Adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

step_size_train = train_generator.n//train_generator.batch_size
model_15.fit_generator(generator = train_generator, steps_per_epoch = step_size_train, epochs = 8, verbose = 2)<jupyter_output>Epoch 1/8
h:\Program Data\Documents\Visual Studio 2019\Projects\ml-test\venv\lib\site-packages\tensorflow\python\keras\engine\training.py:1844: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  warnings.warn('`Model.fit_generator` is deprecated and '
4/4 - 6s - loss: 1.4755 - accuracy: 0.7311
Epoch 2/8
4/4 - 5s - loss: 0.3389 - accuracy: 0.8992
Epoch 3/8
4/4 - 5s - loss: 0.1412 - accuracy: 0.9076
Epoch 4/8
4/4 - 6s - loss: 0.1082 - accuracy: 0.8984
Epoch 5/8
4/4 - 6s - loss: 0.0743 - accuracy: 0.9766
Epoch 6/8
4/4 - 5s - loss: 0.0441 - accuracy: 1.0000
Epoch 7/8
4/4 - 5s - loss: 0.0070 - accuracy: 1.0000
Epoch 8/8
4/4 - 5s - loss: 3.8319e-04 - accuracy: 1.0000<jupyter_text>50 img for train<jupyter_code>model_50 = Model(inputs = base_model.input, outputs = preds)

for layer in model_50.layers:
    layer.trainable = False
for layer in model_50.layers[:20]:
    layer.trainable = False
for layer in model_50.layers[20:]:
    layer.trainable = True
train_datagen = ImageDataGenerator(preprocessing_function = preprocess_input) # included in our dependencies

train_generator = train_datagen.flow_from_directory(
    'data/archive/images/50-n02085620-Chihuahua/', 
    target_size=(224,224), 
    color_mode='rgb', 
    batch_size=32, 
    class_mode='categorical', 
    shuffle=True)
model_50.compile(optimizer = 'Adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

step_size_train = train_generator.n//train_generator.batch_size
model_50.fit_generator(generator = train_generator, steps_per_epoch = step_size_train, epochs = 8, verbose = 2)<jupyter_output>Epoch 1/8
4/4 - 7s - loss: 2.6007 - accuracy: 0.5500
Epoch 2/8
4/4 - 5s - loss: 0.7343 - accuracy: 0.7833
Epoch 3/8
4/4 - 5s - loss: 0.3764 - accuracy: 0.9297
Epoch 4/8
4/4 - 5s - loss: 0.1134 - accuracy: 0.9417
Epoch 5/8
4/4 - 5s - loss: 0.0223 - accuracy: 1.0000
Epoch 6/8
4/4 - 6s - loss: 0.0140 - accuracy: 1.0000
Epoch 7/8
4/4 - 5s - loss: 0.0018 - accuracy: 1.0000
Epoch 8/8
4/4 - 5s - loss: 1.0795e-04 - accuracy: 1.0000<jupyter_text>100 img for train<jupyter_code>model_100 = Model(inputs = base_model.input, outputs = preds)

for layer in model_100.layers:
    layer.trainable = False
for layer in model_100.layers[:20]:
    layer.trainable = False
for layer in model_100.layers[20:]:
    layer.trainable = True
train_datagen = ImageDataGenerator(preprocessing_function = preprocess_input) # included in our dependencies

train_generator = train_datagen.flow_from_directory(
    'data/archive/images/100-n02106662-German_shepherd/', 
    target_size = (224,224), 
    color_mode = 'rgb', 
    batch_size = 32, 
    class_mode = 'categorical', 
    shuffle = True)
model_100.compile(optimizer = 'Adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

step_size_train = train_generator.n//train_generator.batch_size
model_100.fit_generator(generator = train_generator, steps_per_epoch = step_size_train, epochs = 8, verbose = 2)<jupyter_output>Epoch 1/8
4/4 - 7s - loss: 4.2429 - accuracy: 0.6417
Epoch 2/8
4/4 - 5s - loss: 0.3945 - accuracy: 0.7833
Epoch 3/8
4/4 - 6s - loss: 0.3420 - accuracy: 0.8667
Epoch 4/8
4/4 - 5s - loss: 0.1865 - accuracy: 0.9500
Epoch 5/8
4/4 - 5s - loss: 0.0872 - accuracy: 0.9417
Epoch 6/8
4/4 - 5s - loss: 0.0802 - accuracy: 0.9667
Epoch 7/8
4/4 - 5s - loss: 0.0089 - accuracy: 1.0000
Epoch 8/8
4/4 - 5s - loss: 0.0252 - accuracy: 0.9917<jupyter_text>Test<jupyter_code># test 15
m = {}
borzoi_test = "data/archive/images/15-n02090622-borzoi/test/"
for path in pathlib.Path(borzoi_test).iterdir():
    if path.is_file():
        # print(path)
        new_image = load_image(path)
        pred = model_15.predict(new_image)
        # print(pred)
        m[path] = pred[0]

runtime_error = []
value_error = []
for key, value in m.items():
    runtime_error.append(value[0])
    value_error.append(value[1])
data_15 = {
    'Image': list(m.keys()),
    'Runtime Error': list(runtime_error),
    'Value Error': list(value_error)
}
df_15 = pd.DataFrame.from_dict(data_15)
dfAIC = df_15.sort_values(by=['Runtime Error'])
# test 50
m = {}
chihuahua_test = "data/archive/images/50-n02085620-Chihuahua/test"
for path in pathlib.Path(chihuahua_test).iterdir():
    if path.is_file():
        # print(path)
        new_image = load_image(path)
        pred = model_50.predict(new_image)
        # print(pred)
        m[path] = pred[0]

runtime_error = []
value_error = []
for key, value in m.items():
    runtime_error.append(value[0])
    value_error.append(value[1])
data_50 = {
    'Image': list(m.keys()),
    'Runtime Error': list(runtime_error),
    'Value Error': list(value_error)
}
df_50 = pd.DataFrame.from_dict(data_50)
dfAIC = df_50.sort_values(by=['Runtime Error'])
# test 100
m = {}
shepherd_test = "data/archive/images/100-n02106662-German_shepherd/test/"
for path in pathlib.Path(shepherd_test).iterdir():
    if path.is_file():
        # print(path)
        new_image = load_image(path)
        pred = model_100.predict(new_image)
        # print(pred)
        m[path] = pred[0]

runtime_error = []
value_error = []
for key, value in m.items():
    runtime_error.append(value[0])
    value_error.append(value[1])
data_100 = {
    'Image': list(m.keys()),
    'Runtime Error': list(runtime_error),
    'Value Error': list(value_error)
}
df_100 = pd.DataFrame.from_dict(data_100)
dfAIC = df_100.sort_values(by=['Runtime Error'])
df_15.head(10)
df_50.head(10)
df_100.head(10)<jupyter_output><empty_output><jupyter_text>function to load image<jupyter_code>def load_image(img_path):
    img = image.load_img(img_path, target_size=(150, 150))
    img_tensor = image.img_to_array(img)                    # (height, width, channels)
    img_tensor = np.expand_dims(img_tensor, axis=0)         # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
    img_tensor /= 255.                                      # imshow expects values in the range [0, 1]
    return img_tensor<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup