<jupyter_start><jupyter_text>DenseNet121 trained from scratch<jupyter_code>from tensorflow.keras.applications import NASNetLarge

base_model = DenseNet121(include_top=False, weights=None, 
                         input_shape=(img_dim,img_dim,3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
# x = Dense(128, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(nb_classes, activation=None)(x)
predictions = Activation('softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
# model.summary()
import math
top_weights_path = f"{created_data_path}/weights/{modelname}.h5"
model.load_weights(top_weights_path)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
pred = model.predict(valid_generator(X_test.tolist(), y_test.tolist(), batch_size=batch_size, nb_classes=nb_classes, img_dim=img_dim), 
                         steps= len(X_test) / batch_size, 
                         verbose=1)
y_pred = np.argmax(pred, axis=1)
def get_labelencoder_mapping(le):
    '''
    Return a dict mapping labels to their integer values
    from an SKlearn LabelEncoder
    le = a fitted SKlearn LabelEncoder
    '''
    res = {}
    for cl in le.classes_:
        res.update({cl:le.transform([cl])[0]})

    return res

get_labelencoder_mapping(le)
name_map = dict(zip(le.transform(le.classes_), le.classes_))
print(name_map)<jupyter_output>{0: 'bl', 1: 'bv', 2: 'c', 3: 'm', 4: 'sw', 5: 't', 6: 'v', 7: 'v(cy)', 8: 'wmv'}<jupyter_text>Confusion matrix<jupyter_code>from sklearn.metrics import accuracy_score, balanced_accuracy_score, confusion_matrix
import seaborn as sns
print(balanced_accuracy_score(y_test, y_pred))


plt.figure(figsize=(24,14))
cm = confusion_matrix(le.fit_transform(y_test), y_pred)
cm = cm / cm.astype(np.float).sum(axis=0)
sns.heatmap(cm, annot=True, xticklabels=list(name_map.values()), yticklabels=list(name_map.values()))
sns.set(font_scale=1.4)
pd.DataFrame(pred).sort_values(by=8, ascending=False).boxplot(showfliers=False)<jupyter_output><empty_output><jupyter_text>Classification report<jupyter_code>from sklearn.metrics import classification_report

print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.87      0.85      0.86       311
           1       0.69      0.50      0.58       167
           2       0.91      0.86      0.88       233
           3       0.85      0.81      0.83       613
           4       0.60      0.46      0.52       175
           5       0.83      0.77      0.80       292
           6       0.59      0.76      0.66       563
           7       0.63      0.45      0.53       430
           8       0.63      0.78      0.70       489

    accuracy                           0.72      3273
   macro avg       0.73      0.69      0.71      3273
weighted avg       0.73      0.72      0.72      3273<jupyter_text>Actual number of flies vs Predicted by model<jupyter_code>trgt = 8 # wmv code
acts, pred, pnames = [],[],[]
df_test['y_pred'] = y_pred
for i, sub in df_test.groupby('insect_plate_names'):
    acts.append(sub.actual_number.iloc[0])
    pred.append(sub[sub.y_pred == trgt].shape[0])
    pnames.append(sub.insect_plate_names.iloc[0])
#     print(f"\nplate: {sub.insect_plate_names.iloc[0]} \nactual: {sub.actual_number.iloc[0]}, predicted: {sub[sub.y_pred == trgt].shape[0]}")
#     break
# sns.scatterplot(pred, acts)
df_plot = pd.DataFrame({"acts": acts, "pred": pred})

plt.figure(figsize=(14,10))
ax = sns.scatterplot(x="pred", y="acts", data=df_plot, ci=None, s=100);
ident = [0.0, 140.0]
plt.plot(ident,ident, ":")
plt.title("Number of WMV - ρ=0.92")
plt.xlabel("PREDICTED", fontsize=14)
plt.ylabel("ACTUAL", fontsize=14)
# plt.legend(['ρ=0.92'])

from scipy.stats import pearsonr
from numpy import cov

covariance = cov(df_plot.acts, df_plot.pred)
print(f"covariance matrix: \n{covariance}\n")

corr, _ = pearsonr(df_plot.acts, df_plot.pred)
print('Pearsons correlation: %.3f' % corr)
print()

ax.tick_params(axis='both', which='major', labelsize=14)
# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

# Only show ticks on the left and bottom spines
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
df_plot['platenames'] = pnames
df_plot[['platenames','acts','pred']].set_index('platenames').sort_values(by='acts', ascending=False)
data1<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1401259559154296, 'fraction_numerical': 0.049257759784075573, 'mean_word_length': 3.4117063492063493, 'pattern_counts': {'":': 3, '<': 15, '<?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': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '425835', 'license': 'Apache-2.0', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1609, 'n_words': 303, 'path': 'PlottingConfusionMatrices.ipynb', 'repo_name': 'kalfasyan/yannis_impy', 'size': '4446'}