<jupyter_start><jupyter_text>Try EfficientNet B0 (the base model)<jupyter_code>X, Y = [], []

dataset = pd.read_csv('ground-truth.csv')
EPOCHS = 15
LEARNING_RATE = 1e-3

for index, row in dataset.iterrows():
    X.append(array(import_dataset(row[0]).resize((100, 100))).flatten() / 255.0)
    Y.append(row[1])

X = np.array(X)
Y = to_categorical(Y, 2)
X = X.reshape(-1, 100, 100, 3)
X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.20, random_state=5)

#Load EfficientNet, also set drop_connect_rate for stronger regularization
#input layer
inputs = layers.Input(shape=(100,100,3))
#declare EfficientNetB0
model = EfficientNetB0(weights='imagenet', include_top=False, input_tensor=inputs, input_shape=(100, 100, 3), drop_connect_rate=0.4)
model.trainable = False
#rebuild top layers
x = layers.GlobalAveragePooling2D(name="avg_pool")(model.output)
x = layers.BatchNormalization()(x)
top_dropout_rate = 0.2
x = layers.Dropout(top_dropout_rate, name="top_dropout")(x)
outputs = layers.Dense(2, activation="softmax", name="pred")(x)
model = Model(inputs=inputs, outputs=outputs, name="EfficientNet")
model.summary()

optimizer = optimizers.Adam(lr=LEARNING_RATE, epsilon=None)


#hyperparameter settings
epochs = EPOCHS
batch_size = 16
optimizer = optimizers.Adam(lr=LEARNING_RATE, epsilon=None)
model.compile(optimizer=optimizer, loss="mean_squared_error",metrics=['accuracy'])

lr_sched = step_decay_schedule(initial_lr=1e-4, decay_factor=0.75, step_size=2)

history = model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_val, Y_val), verbose=2,callbacks=[lr_sched])<jupyter_output><empty_output><jupyter_text>Set random ID for current training<jupyter_code>import random
#assign random code to make unique ID
r = random.randint(1, 1000)
print(f'Your unique ID is {r}, refer to this ID to check the latest data!')
ID = f'{LEARNING_RATE}n{EPOCHS}_{r}'
#plotting
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")
    
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

#plt.subplot(2,1,1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.ylabel('accuracy')
plt.xlabel('epoch')

plt.savefig(f'plottings\\effnet\\acc_{ID}.png')
plt.show()

#plt.subplot(2,1,2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.savefig(f'plottings\\effnet\\loss_{ID}.png')
plt.show()

Y_pred = model.predict(X_val)
# Convert predictions classes to one hot vectors
Y_pred_classes = np.argmax(Y_pred, axis=1)
# Convert validation observations to one hot vectors
Y_true = np.argmax(Y_val, axis=1)
# compute the confusion matrix
confusion_mtx = confusion_matrix(Y_true, Y_pred_classes)
# plot the confusion matrix
plot_confusion_matrix(confusion_mtx, classes=range(2))
plt.savefig(f'plottings\\effnet\\cnfmtx_{ID}.png')
plt.show()
def unfreeze_model(model):
    # We unfreeze the top 20 layers while leaving BatchNorm layers frozen
    for layer in model.layers[-20:]:
        if not isinstance(layer, layers.BatchNormalization):
            layer.trainable = True

    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)
    model.compile(
        optimizer=optimizer, loss="categorical_crossentropy", metrics=["accuracy"]
    )

unfreeze_model(model)

epochs = EPOCHS  # @param {type: "slider", min:8, max:50}
history = model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, validation_data=(X_val, Y_val), verbose=2,callbacks=[lr_sched])

#plotting
def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j], horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")
    
    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

#plt.subplot(2,1,1)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.ylabel('accuracy')
plt.xlabel('epoch')

plt.savefig(f'plottings\\effnet\\ftacc_{ID}.png')
plt.show()

#plt.subplot(2,1,2)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.savefig(f'plottings\\effnet\\ftloss_{ID}.png')
plt.show()

Y_pred = model.predict(X_val)
# Convert predictions classes to one hot vectors
Y_pred_classes = np.argmax(Y_pred, axis=1)
# Convert validation observations to one hot vectors
Y_true = np.argmax(Y_val, axis=1)
# compute the confusion matrix
confusion_mtx = confusion_matrix(Y_true, Y_pred_classes)
# plot the confusion matrix
plot_confusion_matrix(confusion_mtx, classes=range(2))
plt.savefig(f'plottings\\effnet\\ftcnfmtx_{ID}.png')
plt.show()
#save model
model_name = f'models\\effnet\\{ID}.h5'
model.save(model_name)
    
train_acc, val_acc = np.mean(history.history['accuracy']), np.mean(history.history['val_accuracy'])
print(train_acc, val_acc)
with open('currently-used-effnet.txt','w+') as f:
    f.write(model_name)<jupyter_output><empty_output><jupyter_text>Testing<jupyter_code>from tensorflow.keras.models import load_model
import tensorflow as tf
#test_set = glob.glob('MICC-F220\\*')
test_set = glob.glob('test_set\\*')
classes = []
for img in test_set:
    #data will have the label `tamp` if the data is tampered
    if 'tamp' in img and img[len(img)-4:len(img)] == '.jpg':
        classes.append('forged')
    elif 'tamp' not in img and img[len(img)-4:len(img)] == '.jpg':
        classes.append('unforged')

#for i in range(len(test_set)): print(test_set[i], classes[i])
#def predict():
#load model
model = load_model(model_name)
predictions = []
#test image iteration
for i in tf.range(len(test_set)): #use tf.range for faster execution (operation is done in tf.Tensor)
    image = Image.open(test_set[i]).convert('RGB')
    image = array(image.resize((100,100))).flatten() / 255.0
    image = image.reshape(-1,100,100,3)
    pred = model.predict(image)[0]
    #take the index of the predicted class
    index = np.argmax(pred)
    verdict = "forged" if index == 1 else "unforged"
    predictions.append(verdict)
correct = 0
for i in range(len(classes)):
    if classes[i] == predictions[i]:
        correct += 1
    else: continue
acc = correct / len(classes)
print(f'Test accuracy: {round(acc*100,3)}%')
with open(f'plottings\\effnet\\testacc_{ID}.txt','w+') as f:
    f.write(f'train_acc: {train_acc}\nval_acc: {val_acc}\n')
    f.write(f'test_acc: {acc}')
#for i in range(len(classes)): print(classes[i], predictions[i])
correct = 0
plt.figure(figsize=(10,9))
plt.subplots_adjust(hspace=0.5)
for i in range(len(classes)):
    im = cv2.imread(test_set[i])
    #im.resize((30,30))
    #print(im.shape)
    plt.subplot(len(classes)/5+1,5,i+1)
    plt.imshow(im)
    color = 'green' if classes[i] == predictions[i] else 'red'
    if color == 'green': correct += 1
    #plt.title(f'{classes[i]}\n{predictions[i]}', color=color)
    plt.text(im.shape[1]/2,0,f'{classes[i]}\n', ha="center", va="bottom", size="medium")
    plt.text(im.shape[1]/2,0,f'\n{predictions[i]}', ha="center", va="bottom", size="medium",color=color)
    plt.axis('off')

plt.savefig(f'plottings\\vgg19\\test_{ID}.png')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16225490196078432, 'fraction_numerical': 0.019852941176470587, 'mean_word_length': 5.3907595927956145, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '519291', 'license': 'MIT', 'n_tokens_mistral': 3152, 'n_tokens_neox': 3016, 'n_words': 583, 'path': 'efficientnet.ipynb', 'repo_name': 'sugaarrrr/deep-image-forgery-detection', 'size': '8160'}