text_plain_output_1.png
from keras.callbacks import ModelCheckpoint

from keras.preprocessing.image import ImageDataGenerator

from sklearn.model_selection import train_test_split

from sklearn.preprocessing import MultiLabelBinarizer

from tensorflow.keras.layers import Input, Dense

from tensorflow.keras.models import Model

from vit_keras import vit, utils

import csv

import cv2 as cv

import matplotlib.pyplot as plt

import numpy as np

import os

import re

X = []
Y = []
input_shape = (96, 96, 3)
path_to_subset = f'../input/apparel-images-dataset/'
for folder in os.listdir(path_to_subset):
    for image in os.listdir(os.path.join(path_to_subset, folder)):
        path_to_image = os.path.join(path_to_subset, folder, image)
        image = cv.imread(path_to_image)
        image = cv.resize(image, (input_shape[1], input_shape[0]))
        label = re.findall('\\w+\\_\\w+', path_to_image)[0].split('_')
        X.append(image)
        Y.append(label)
X = np.array(X) / 255.0
Y = np.array(Y)
mlb = MultiLabelBinarizer()
Y = mlb.fit_transform(Y)

x, test_x, y, test_y = train_test_split(X, Y, test_size=0.1, stratify=Y, shuffle=True, random_state=1)
train_x, val_x, train_y, val_y = train_test_split(x, y, test_size=0.2, stratify=y, shuffle=True, random_state=1)
datagen = ImageDataGenerator(rotation_range=45, width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.2, horizontal_flip=True, validation_split=0.2)

input_shape = (96, 96, 3)
num_classes = Y.shape[1]
vit_model = vit.vit_b16(image_size=input_shape[0], activation='softmax', pretrained=True, include_top=False, pretrained_top=False, classes=num_classes, weights='imagenet21k')
for layer in vit_model.layers:
    layer.trainable = False
output_layer = Dense(num_classes, activation='sigmoid')(vit_model.output)
model = Model(inputs=vit_model.input, outputs=output_layer)

checkpoint = ModelCheckpoint('../working/visiontransformermodel.tf', save_best_only=True, monitor='val_loss', verbose=1)
batch_size = 64
epochs = 100
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(datagen.flow(train_x, train_y, batch_size=batch_size, subset='training'), validation_data=datagen.flow(train_x, train_y, batch_size=batch_size, subset='validation'), epochs=epochs, callbacks=[checkpoint])

training_acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
filename = '../working/accuracy.csv'
with open(filename, 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(['Epoch', 'Training Accuracy', 'Validation Accuracy'])
    for epoch, (train_acc, val_acc) in enumerate(zip(training_acc, val_acc), 1):
        csvwriter.writerow([epoch, train_acc, val_acc])

training_loss = history.history['loss']
val_loss = history.history['val_loss']
filename = '../working/loss.csv'
with open(filename, 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(['Epoch', 'loss', 'val_loss'])
    for epoch, (loss, val_loss) in enumerate(zip(training_loss, val_loss), 1):
        csvwriter.writerow([epoch, loss, val_loss])

filename = "../working/accuracy.csv"

epoch = []
train_acc = []
val_acc = []
with open(filename, 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    header = next(csvreader)
    for row in csvreader:
        epoch.append(int(row[0]))
        train_acc.append(float(row[1]))
        val_acc.append(float(row[2]))

fig = plt.figure(figsize=(20,7))
plt.subplot(121)
plt.plot(epoch, train_acc, label='acc')
plt.plot(epoch, val_acc, label='val_acc')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.grid()
plt.legend()
plt.show()

filename = '../working/loss.csv'
epoch = []
train_loss = []
val_loss = []
with open(filename, 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    header = next(csvreader)
    for row in csvreader:
        epoch.append(int(row[0]))
        train_loss.append(float(row[1]))
        val_loss.append(float(row[2]))
fig = plt.figure(figsize=(20, 7))
plt.subplot(121)
plt.plot(epoch, train_loss, label='loss')
plt.plot(epoch, val_loss, label='val_loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.grid()
plt.legend()
plt.show()