<jupyter_start><jupyter_text>2. Classify images with XceptionTrained on CelebA [dataset](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html).<jupyter_code>import os

import pandas as pd
import numpy as np
import cv2
import matplotlib.pyplot as plt

import keras
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
from keras.applications import Xception
from keras import layers
from keras.applications.xception import preprocess_input

from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>Xception training and testingCelebA dataset has to be downloaded manualy from [google drive](https://drive.google.com/drive/folders/0B7EVK8r0v71pbWNEUjJKdDQ3dGc?usp=sharing).Extract the archives and specify the path to the dataset in **\_image_dir** variable.**\_num_epochs** or **\_batch_size** can be custom specified.You can also specify the **\_attributes** you want to train on, but we recommend the default for the first run.<jupyter_code>_image_dir = '/data/rbelanec/datasets/celeba/img_align_celeba_png/' # replace this with path to raw CelebA dataset
_img_width = 128
_img_height = 128
_batch_size = 20
_num_epochs = 10
_attributes = ['Eyeglasses', 'Smiling', 'Male', 'No_Beard', 'Young']
_weights_name = 'weights/weights_xception_5attr.h5'<jupyter_output><empty_output><jupyter_text>Data preparationSelect only desired attributes, add partition to each image and add fullpath to images.This partitiong is recommended by CelebA dataset authors.<jupyter_code>img_df = pd.read_csv('../dataset/celeba/attr_celeba.csv').replace({-1:0})[['filename'] + _attributes]
partition_df = pd.read_csv('../dataset/celeba/list_eval_partition.csv')

img_df = img_df.merge(partition_df, on='filename') # add partition merge dataframes on filename
img_df.loc[:, 'filename'] = img_df.loc[:, 'filename'].apply(lambda x: os.path.join(_image_dir, x)) # full path
img_df.head()
# split to train, valid and test based on partitions
train_df = img_df.loc[img_df.loc[:, 'partition'] == 0, ['filename'] + _attributes]
valid_df = img_df.loc[img_df.loc[:, 'partition'] == 1, ['filename'] + _attributes]
test_df = img_df.loc[img_df.loc[:, 'partition'] == 2,  ['filename'] + _attributes]<jupyter_output><empty_output><jupyter_text>We are using ImageDataGenerators to load dataset from dataframe.<jupyter_code>train_datagen =  ImageDataGenerator(
    preprocessing_function=preprocess_input,
    rescale=1./255,
)

train_generator = train_datagen.flow_from_dataframe(
    train_df,
    batch_size=_batch_size,
    x_col='filename', 
    y_col=_attributes,
    class_mode='raw',
    target_size=(_img_width, _img_height),
    validate_filenames=False)
valid_datagen =  ImageDataGenerator(
    preprocessing_function=preprocess_input,
    rescale=1./255
)

valid_generator = valid_datagen.flow_from_dataframe(
    valid_df,
    batch_size=_batch_size,
    x_col='filename', 
    y_col=_attributes,
    class_mode='raw',
    target_size=(_img_width, _img_height),
    validate_filenames=False)<jupyter_output><empty_output><jupyter_text>Model creationXception base model is pretrained on imagenet dataset.<jupyter_code>base_model = Xception(
             input_shape=(_img_width, _img_height, 3), 
             include_top=False, 
             weights="imagenet"
        )<jupyter_output><empty_output><jupyter_text>Adding average pooling layer and output layer.<jupyter_code>x = base_model.output
x = layers.GlobalAveragePooling2D()(x)
outputs = layers.Dense(len(_attributes), activation="sigmoid")(x)<jupyter_output><empty_output><jupyter_text>Compile model. Learning rate can be also custom specified, but for the first run, we recommend the default value.<jupyter_code>model = keras.Model(inputs=base_model.input, outputs=outputs)
model.compile(optimizer=keras.optimizers.Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Save best validation loss.<jupyter_code>checkpointer = ModelCheckpoint(filepath=_weights_name, verbose=1, save_best_only=True)<jupyter_output><empty_output><jupyter_text>Model training<jupyter_code>history = model.fit(train_generator,
                    validation_data=valid_generator,
                    steps_per_epoch=len(train_df)//_batch_size,
                    validation_steps=len(valid_df) // _batch_size,
                    epochs=10,
                    callbacks=[checkpointer],
                    verbose=1)
plt.figure(figsize=(18, 4))
plt.plot(history.history['loss'], label = 'train')
plt.plot(history.history['val_loss'], label = 'valid')
plt.legend()
plt.title('Loss Function')
plt.show()
plt.figure(figsize=(18, 4))
plt.plot(history.history['accuracy'], label = 'train')
plt.plot(history.history['val_accuracy'], label = 'valid')
plt.legend()
plt.title('Accuracy')
plt.show()<jupyter_output><empty_output><jupyter_text>Optional: Skip Training<jupyter_code>! mkdir weights
! cd weights && wget -nc data.belanec.eu/xception/weights/weights_xception_5attr.h5<jupyter_output><empty_output><jupyter_text>Model testing<jupyter_code>model.load_weights(_weights_name)
test_datagen =  ImageDataGenerator(
    preprocessing_function=preprocess_input,
    rescale=1./255,
)

test_generator = test_datagen.flow_from_dataframe(
    test_df,
    batch_size=1,
    x_col='filename', 
    y_col=attributes,
    class_mode='raw',
    target_size=(_img_width, _img_height),
    validate_filenames=False,
    shuffle=False)
model_predictions = model.predict(test_generator)
preds = np.array(model_predictions > 0.5).astype(int)
test_accuracy = 100 * accuracy_score(preds, test_df.loc[:, attributes].values)
print('Test accuracy score: ' + str(test_accuracy))
f1 = f1_score(test_df.loc[:,  attributes].values, preds, average='weighted')
print('f1 score: ' + str(f1))<jupyter_output><empty_output><jupyter_text>Generated images classification Data preparation<jupyter_code>label_datagen =  ImageDataGenerator(
    preprocessing_function=preprocess_input,
    rescale=1./255,
)

label_generator = label_datagen.flow_from_directory(
    '../../01_generate_samples/img', # path to generated images
    classes=['to_label'], # to_label is the last directory
    batch_size=1,
    target_size=(_img_width, _img_height),
    shuffle=False)<jupyter_output><empty_output><jupyter_text>Label prediction<jupyter_code>model_predictions = model.predict(label_generator)
np.set_printoptions(formatter={'float_kind':"{:.6f}".format})
print(model_predictions[0])<jupyter_output><empty_output><jupyter_text>Labels are representing a probability of: Eyeglasses, Smiling, Male, No_Beard, Young<jupyter_code>import h5py
with h5py.File('sample_z2.h5', 'r') as f:
    z = f['z'][:]
from custom_gen import StyleGANGenerator
_stylegan_ffhq_f_gdrive_url = 'https://nvlabs-fi-cdn.nvidia.com/stylegan2/networks/stylegan2-ffhq-config-f.pkl'

gen = StyleGANGenerator(_stylegan_ffhq_f_gdrive_url)
img = gen.get_images(np.reshape(z[0], (1,512)))
plt.imshow(img[0]/255)<jupyter_output><empty_output><jupyter_text>Predictions for the image above: - Eyeglasses: **2.3424625e-05** - Smiling: **9.7760320e-01** - Male: **3.3760071e-04**- No_Beard: **9.9998659e-01**- Young: **8.0151093e-01**<jupyter_code>import pickle
with open('predictions/predictions_xception_5attr.pkl', 'wb') as f:
    pickle.dump(model_predictions, f)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12087168381158636, 'fraction_numerical': 0.019355711965349215, 'mean_word_length': 6.104807692307692, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 62, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '442192', 'license': 'MIT', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2514, 'n_words': 448, 'path': '02_classifiers/xception/xception.ipynb', 'repo_name': 'Wicwik/thesis-official', 'size': '7388'}