<jupyter_start><jupyter_text>Training with k-fold Validation<jupyter_code>X.shape, (y == 0).sum(), (y == 1).sum()
nos = X.shape[0]
bs = int(nos/8)

numit = NumpyArrayIterator(
    X, y, datagen, batch_size=nos, shuffle=False, sample_weight=None,
    seed=None, data_format=None, save_to_dir=None, save_prefix='',
    save_format='png', subset=None, dtype=None
)

for samples_batch,labels_batch in numit:
    print(samples_batch.shape)
    X[::] = samples_batch
    break
from sklearn.model_selection import StratifiedKFold
skf = StratifiedKFold(n_splits=8)
accuracies = []
f = 1

for train_index, test_index in skf.split(X, y):
        X_train = X[train_index]
        X_test = X[test_index]
        y_train = y[train_index]
        y_test = y[test_index]
      
        X_train1 = conv_base1.predict(X_train)
        X_train1 = X_train1.reshape(X_train.shape[0],9*9*512)
        X_test1 = conv_base1.predict(X_test)
        X_test1 = X_test1.reshape(X_test.shape[0],9*9*512)
        
        X_train2 = conv_base2.predict(X_train)
        X_train2 = X_train2.reshape(X_train.shape[0],9*9*1024)
        X_test2 = conv_base2.predict(X_test)
        X_test2 = X_test2.reshape(X_test.shape[0],9*9*1024)
        
        X_train3 = conv_base3.predict(X_train)
        X_train3 = X_train3.reshape(X_train.shape[0],9*9*1920)
        X_test3 = conv_base3.predict(X_test)
        X_test3 = X_test3.reshape(X_test.shape[0],9*9*1920)

        model1 = models.Sequential()
        model1.add(layers.Dense(256, activation = 'relu', input_dim = 9*9*512))
        model1.add(layers.Dropout(0.5))
        model1.add(layers.Dense(1, activation = 'sigmoid'))
        model1.compile(optimizer = optimizers.Adam(lr = 1e-3),loss = 'binary_crossentropy',metrics = ['accuracy'])
        
        model2 = models.Sequential()
        model2.add(layers.Dense(256, activation = 'relu', input_dim = 9*9*1024))
        model2.add(layers.Dropout(0.5))
        model2.add(layers.Dense(1, activation = 'sigmoid'))
        model2.compile(optimizer = optimizers.Adam(lr = 1e-3),loss = 'binary_crossentropy',metrics = ['accuracy'])
        
        model3 = models.Sequential()
        model3.add(layers.Dense(256, activation = 'relu', input_dim = 9*9*1920))
        model3.add(layers.Dropout(0.5))
        model3.add(layers.Dense(1, activation = 'sigmoid'))
        model3.compile(optimizer = optimizers.Adam(lr = 1e-3),loss = 'binary_crossentropy',metrics = ['accuracy'])
        
        
        my_callbacks = [EarlyStopping(monitor = 'val_loss', patience=5, restore_best_weights=True)]

        print('Fold {}'.format(f))
        
        history1 = model1.fit(X_train1, y_train, epochs = 100, batch_size = 20, validation_data = (X_test1, y_test), shuffle = True, callbacks = my_callbacks)
        history2 = model2.fit(X_train2, y_train, epochs = 100, batch_size = 20, validation_data = (X_test2, y_test), shuffle = True, callbacks = my_callbacks)
        history3 = model3.fit(X_train3, y_train, epochs = 100, batch_size = 20, validation_data = (X_test3, y_test), shuffle = True, callbacks = my_callbacks)


        test_preds1 = model1.predict(X_test1, batch_size = 20)
        test_predclass1 = np.array([1.0 if i>0.5 else 0.0 for i in test_preds1])

        test_preds2 = model2.predict(X_test2, batch_size = 20)
        test_predclass2 = np.array([1.0 if i>0.5 else 0.0 for i in test_preds2])

        test_preds3 = model3.predict(X_test3, batch_size = 20)
        test_predclass3 = np.array([1.0 if i>0.5 else 0.0 for i in test_preds3])

        kfinal_preds = np.empty(test_predclass3.shape[0])
        for i in range(test_predclass3.shape[0]):
          predsum = test_predclass1[i] + test_predclass2[i] + test_predclass3[i] 
          kfinal_preds[i] = 0.0 if predsum in [0.0,1.0] else 1.0


        accuracies.append([confusion_matrix(y_test,kfinal_preds)[0][0]/(y_test == 0.0).sum(), confusion_matrix(y_test,kfinal_preds)[1][1]/(y_test == 1.0).sum()])
        f+=1
zero_acc = 0
one_acc = 0

for i in accuracies:
  zero_acc+= i[0]
  one_acc+=i[1]

zero_acc/=8
one_acc/=8

print(zero_acc, one_acc)<jupyter_output>0.758477011494253 0.725<jupyter_text>Training on whole Training Dataset<jupyter_code>conv1_samples = np.zeros(shape=(nos,9,9,512))
conv1_labels = np.zeros(shape=(nos))

for samples_batch,labels_batch in numit:
    f = conv_base1.predict(samples_batch)
    print(f.shape)
    conv1_samples = f
    conv1_labels = labels_batch
    break
conv1_samples = conv1_samples.reshape(nos,9*9*512)
conv2_samples = np.zeros(shape=(nos,9,9,1024))
conv2_labels = np.zeros(shape=(nos))
i = 0
for samples_batch,labels_batch in numit:
    f = conv_base2.predict(samples_batch)
    print(f.shape)
    conv2_samples = f
    conv2_labels = labels_batch
    break
conv2_samples = conv2_samples.reshape(nos,9*9*1024)
conv3_samples = np.zeros(shape=(nos,9,9,1920))
conv3_labels = np.zeros(shape=(nos))
i = 0
for samples_batch,labels_batch in numit:
    f = conv_base3.predict(samples_batch)
    print(f.shape)
    conv3_samples = f
    conv3_labels = labels_batch
    break
conv3_samples = conv3_samples.reshape(nos,9*9*1920)
conv1_samples.shape, conv1_labels.shape, conv2_samples.shape, conv2_labels.shape, conv3_samples.shape, conv3_labels.shape




model_conv1 = models.Sequential()
model_conv1.add(layers.Dense(256, activation = 'relu', input_dim = 9*9*512))
model_conv1.add(layers.Dropout(0.5))
model_conv1.add(layers.Dense(1, activation = 'sigmoid'))
model_conv1.compile(optimizer = optimizers.Adam(lr = 1e-3),loss = 'binary_crossentropy',metrics = ['accuracy'])

model_conv2 = models.Sequential()
model_conv2.add(layers.Dense(256, activation = 'relu', input_dim = 9*9*1024))
model_conv2.add(layers.Dropout(0.5))
model_conv2.add(layers.Dense(1, activation = 'sigmoid'))
model_conv2.compile(optimizer = optimizers.Adam(lr = 1e-3),loss = 'binary_crossentropy',metrics = ['accuracy'])

model_conv3 = models.Sequential()
model_conv3.add(layers.Dense(256, activation = 'relu', input_dim = 9*9*1920))
model_conv3.add(layers.Dropout(0.5))
model_conv3.add(layers.Dense(1, activation = 'sigmoid'))
model_conv3.compile(optimizer = optimizers.Adam(lr = 1e-3),loss = 'binary_crossentropy',metrics = ['accuracy'])

my_callbacks = [EarlyStopping(monitor = 'val_loss', patience=5, restore_best_weights=True)]
xtest = np.load('gdrive/My Drive/pcb/xtest.npy')
ytest = np.load('gdrive/My Drive/pcb/ytest.npy')

datagentest = ImageDataGenerator(
    featurewise_center = True,
    featurewise_std_normalization = True
)
datagentest.fit(xtest)
testnos = ytest.shape[0]


numittest = NumpyArrayIterator(
    xtest, ytest, datagentest, batch_size=testnos, shuffle=False, sample_weight=None,
    seed=None, data_format=None, save_to_dir=None, save_prefix='',
    save_format='png', subset=None, dtype=None
)

conv1_test_samples = np.zeros(shape=(testnos,9,9,512))
conv1_test_labels = np.zeros(shape=(testnos))
for samples_batch,labels_batch in numittest:
    g = conv_base1.predict(samples_batch)
    print(g.shape)
    conv1_test_samples[::] = g
    conv1_test_labels[:] = labels_batch
    break
conv1_test_samples = conv1_test_samples.reshape(testnos,9*9*512)
conv2_test_samples = np.zeros(shape=(testnos,9,9,1024))
conv2_test_labels = np.zeros(shape=(testnos))
for samples_batch,labels_batch in numittest:
    g = conv_base2.predict(samples_batch)
    print(g.shape)
    conv2_test_samples[::] = g
    conv2_test_labels[:] = labels_batch
    break
conv2_test_samples = conv2_test_samples.reshape(testnos,9*9*1024)
conv3_test_samples = np.zeros(shape=(testnos,9,9,1920))
conv3_test_labels = np.zeros(shape=(testnos))
for samples_batch,labels_batch in numittest:
    g = conv_base3.predict(samples_batch)
    print(g.shape)
    conv3_test_samples[::] = g
    conv3_test_labels[:] = labels_batch
    break
conv3_test_samples = conv3_test_samples.reshape(testnos,9*9*1920)
# To save memory
del xtest
del ytest
conv1_historytest = model_conv1.fit(conv1_samples, conv1_labels, epochs = 100, batch_size = 20, validation_data = (conv1_test_samples, conv1_test_labels), shuffle = True,  callbacks = my_callbacks)
conv2_historytest = model_conv2.fit(conv2_samples, conv2_labels, epochs = 100, batch_size = 20, validation_data = (conv2_test_samples, conv2_test_labels), shuffle = True,  callbacks = my_callbacks)
conv3_historytest = model_conv3.fit(conv3_samples, conv3_labels, epochs = 100, batch_size = 20, validation_data = (conv3_test_samples, conv3_test_labels), shuffle = True,  callbacks = my_callbacks)

conv1_test_preds = model_conv1.predict(conv1_test_samples, batch_size = 20)
conv1_test_predclass = np.array([1.0 if i>0.5 else 0.0 for i in conv1_test_preds])
conv2_test_preds = model_conv2.predict(conv2_test_samples, batch_size = 20)
conv2_test_predclass = np.array([1.0 if i>0.5 else 0.0 for i in conv2_test_preds])
conv3_test_preds = model_conv3.predict(conv3_test_samples, batch_size = 20)
conv3_test_predclass = np.array([1.0 if i>0.5 else 0.0 for i in conv3_test_preds])
# Voting

final_preds = np.empty(conv1_test_predclass.shape[0])
for i in range(conv1_test_predclass.shape[0]):
  predsum = conv1_test_predclass[i] + conv2_test_predclass[i] + conv3_test_predclass[i] 
  final_preds[i] = 0.0 if predsum in [0.0,1.0] else 1.0
confusion_matrix(conv1_test_labels,final_preds)[0][0]/(conv1_test_labels == 0.0).sum(), confusion_matrix(conv1_test_labels,final_preds)[1][1]/(conv1_test_labels == 1.0).sum()
# Saving model

# model_json = model.to_json()
# with open("gdrive/My Drive/models/VGG19_12.json", "w") as json_file:
#     json_file.write(model_json)
# model.save_weights("gdrive/My Drive/models/VGG19_12.h5")
# print("Saved model to disk")
# Saving and loading models
# https://machinelearningmastery.com/save-load-keras-deep-learning-models/<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1302740991684632, 'fraction_numerical': 0.05410122164048865, 'mean_word_length': 4.710433763188745, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 1, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '155692', 'license': 'MIT', 'n_tokens_mistral': 3905, 'n_tokens_neox': 3693, 'n_words': 600, 'path': 'VGG19_DN121_DN201_pretrained_Ensemble.ipynb', 'repo_name': 'shouvikcirca/PCB_Defect_Detection', 'size': '9741'}