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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""mp_art_classification.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1mCMy50B9xHW2WdGNlxTq-wObAe-eMsQ5
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import shutil
|
| 12 |
+
import math
|
| 13 |
+
import glob
|
| 14 |
+
import json
|
| 15 |
+
import pickle
|
| 16 |
+
import requests
|
| 17 |
+
import time
|
| 18 |
+
import re
|
| 19 |
+
import string
|
| 20 |
+
from datetime import datetime
|
| 21 |
+
|
| 22 |
+
import pandas as pd
|
| 23 |
+
import numpy as np
|
| 24 |
+
from PIL import Image
|
| 25 |
+
|
| 26 |
+
import matplotlib.pyplot as plt
|
| 27 |
+
|
| 28 |
+
import tensorflow as tf
|
| 29 |
+
|
| 30 |
+
if 'workspace/semantic_search' in os.getcwd():
|
| 31 |
+
ROOT_FOLDER = os.path.join("./hf", "mp_art_classification")
|
| 32 |
+
else:
|
| 33 |
+
ROOT_FOLDER = './'
|
| 34 |
+
|
| 35 |
+
PRE_TRAINED_MODELS_FOLDER = os.path.join(ROOT_FOLDER, "pre_trained_models")
|
| 36 |
+
TRAINED_WEIGHTS_FOLDER = os.path.join(ROOT_FOLDER, "trained_weights")
|
| 37 |
+
|
| 38 |
+
def clean_directories():
|
| 39 |
+
shutil.rmtree(PRE_TRAINED_MODELS_FOLDER, ignore_errors=True)
|
| 40 |
+
|
| 41 |
+
# clean_directories()
|
| 42 |
+
|
| 43 |
+
def create_directories():
|
| 44 |
+
if not os.path.exists(PRE_TRAINED_MODELS_FOLDER):
|
| 45 |
+
os.mkdir(PRE_TRAINED_MODELS_FOLDER)
|
| 46 |
+
|
| 47 |
+
create_directories()
|
| 48 |
+
|
| 49 |
+
from transformers import CLIPTokenizer, CLIPImageProcessor, TFCLIPTextModel, TFCLIPVisionModel
|
| 50 |
+
|
| 51 |
+
clip_model_id = "openai/clip-vit-large-patch14"
|
| 52 |
+
|
| 53 |
+
vision_model = TFCLIPVisionModel.from_pretrained(
|
| 54 |
+
clip_model_id,
|
| 55 |
+
cache_dir=PRE_TRAINED_MODELS_FOLDER)
|
| 56 |
+
vision_processor = CLIPImageProcessor.from_pretrained(clip_model_id)
|
| 57 |
+
|
| 58 |
+
base_learning_rate = 0.0001
|
| 59 |
+
steps_per_execution = 200
|
| 60 |
+
|
| 61 |
+
def create_classification_model():
|
| 62 |
+
|
| 63 |
+
# Preprocess images
|
| 64 |
+
inputs = tf.keras.Input(shape=(3, 224, 224))
|
| 65 |
+
rescaling_layer = tf.keras.layers.Rescaling(1.0/255, offset=0.0)
|
| 66 |
+
rescaled_input = rescaling_layer(inputs)
|
| 67 |
+
# processed_inputs = vision_processor(images=[inputs], return_tensors="tf")
|
| 68 |
+
# print(inputs)
|
| 69 |
+
|
| 70 |
+
vision_model.trainable=False
|
| 71 |
+
# vision_model.trainable = True
|
| 72 |
+
# # Fine-tune from this layer onwards
|
| 73 |
+
# fine_tune_at = 100
|
| 74 |
+
|
| 75 |
+
# # Freeze all the layers before the `fine_tune_at` layer
|
| 76 |
+
# for layer in base_model.layers[:fine_tune_at]:
|
| 77 |
+
# layer.trainable = False
|
| 78 |
+
base_model_output = vision_model(rescaled_input)
|
| 79 |
+
|
| 80 |
+
current_layer = base_model_output.pooler_output
|
| 81 |
+
hidden_layers_nodes = [64]
|
| 82 |
+
for node_count in hidden_layers_nodes:
|
| 83 |
+
hidden_layer = tf.keras.layers.Dense(node_count, activation='relu')
|
| 84 |
+
dropout_layer = tf.keras.layers.Dropout(.2, input_shape=(2,))
|
| 85 |
+
# hidden_layer.trainable = False
|
| 86 |
+
current_layer = hidden_layer(dropout_layer(current_layer))
|
| 87 |
+
|
| 88 |
+
prediction_layer = tf.keras.layers.Dense(9, activation='softmax')
|
| 89 |
+
# prediction_layer.trainable = False
|
| 90 |
+
outputs = prediction_layer(current_layer)
|
| 91 |
+
model = tf.keras.Model(inputs, outputs)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
model.compile(
|
| 95 |
+
# Used leagcy optimizer due to tf 2.11 release issues with MACOS
|
| 96 |
+
# optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
|
| 97 |
+
optimizer=tf.keras.optimizers.legacy.Adam(learning_rate=base_learning_rate),
|
| 98 |
+
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
|
| 99 |
+
metrics=['accuracy'],
|
| 100 |
+
steps_per_execution=steps_per_execution
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
return model
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
model = create_classification_model()
|
| 108 |
+
model.summary()
|
| 109 |
+
|
| 110 |
+
latest_weights = tf.train.latest_checkpoint(TRAINED_WEIGHTS_FOLDER)
|
| 111 |
+
model.load_weights(latest_weights)
|
| 112 |
+
|
| 113 |
+
# image_path = tf.constant('./hf/mp_image_search/examples/e1.jpeg')
|
| 114 |
+
# image = tf.io.read_file("/Users/skoneru/workspace/semantic_search/hf/mp_image_search/examples/e1.jpeg")
|
| 115 |
+
# print(image)
|
| 116 |
+
# decoded_image = tf.io.decode_image(
|
| 117 |
+
# contents = image,
|
| 118 |
+
# channels = 3,
|
| 119 |
+
# expand_animations = False
|
| 120 |
+
# )
|
| 121 |
+
# print(decoded_image.shape)
|
| 122 |
+
# resized_image = tf.image.resize_with_pad(
|
| 123 |
+
# image = decoded_image,
|
| 124 |
+
# target_height = 224,
|
| 125 |
+
# target_width = 224,
|
| 126 |
+
# )
|
| 127 |
+
# print(resized_image.shape)
|
| 128 |
+
# # constant_new = tf.constant(
|
| 129 |
+
# # resized_image, dtype=tf.float32, shape=(224,224,3), name='input_image'
|
| 130 |
+
# # )
|
| 131 |
+
# transposed_image = tf.transpose(
|
| 132 |
+
# resized_image)
|
| 133 |
+
# print(transposed_image.shape)
|
| 134 |
+
# # constant = tf.constant(
|
| 135 |
+
# # transposed_image, value_index=(3,224,224)
|
| 136 |
+
# # )
|
| 137 |
+
# # constant_new = tf.constant(
|
| 138 |
+
# # transposed_image, dtype=tf.float32, shape=(3,224,224), name='input_image'
|
| 139 |
+
# # )
|
| 140 |
+
# ndarray = tf.make_ndarray(
|
| 141 |
+
# tf.Variable(transposed_image, shape=(3,224,224))
|
| 142 |
+
# )
|
| 143 |
+
# # variable = tf.Variable(constant_new)
|
| 144 |
+
# # print(constant_new)
|
| 145 |
+
# # print(inputs)
|
| 146 |
+
|
| 147 |
+
# image_path = './hf/mp_image_search/examples/e1.jpeg'
|
| 148 |
+
# img = Image.open(image_path).convert('RGB')
|
| 149 |
+
# desired_size =224
|
| 150 |
+
# old_size = img.size # old_size[0] is in (width, height) format
|
| 151 |
+
# ratio = float(desired_size)/max(old_size)
|
| 152 |
+
# new_size = tuple([int(x*ratio) for x in old_size])
|
| 153 |
+
|
| 154 |
+
# img.thumbnail((desired_size, desired_size), Image.ANTIALIAS)
|
| 155 |
+
|
| 156 |
+
# new_im = Image.new("RGB", (desired_size, desired_size))
|
| 157 |
+
# new_im.paste(img, ((desired_size-new_size[0])//2,
|
| 158 |
+
# (desired_size-new_size[1])//2))
|
| 159 |
+
|
| 160 |
+
# # new_im.show()
|
| 161 |
+
# np_array = np.array(img)
|
| 162 |
+
# print(np_array.shape)
|
| 163 |
+
# transposed_np_array = np.transpose(np_array)
|
| 164 |
+
# print(transposed_np_array.shape)
|
| 165 |
+
# images_list = []
|
| 166 |
+
# images_list.append(transposed_np_array)
|
| 167 |
+
# np_input = np.asarray(images_list)
|
| 168 |
+
# print(np_input.shape)
|
| 169 |
+
# result = model.predict(np_input)
|
| 170 |
+
# print(result.flatten())
|
| 171 |
+
|
| 172 |
+
genre_classes_path = os.path.join(ROOT_FOLDER,'genre_class.txt')
|
| 173 |
+
# TSV headers [id, class]
|
| 174 |
+
genre_classes_df = pd.read_csv(genre_classes_path, sep = ' ', header=None)
|
| 175 |
+
# print(genre_train_df.iloc[:,1])
|
| 176 |
+
genre_classes = []
|
| 177 |
+
for index, row in genre_classes_df.iterrows():
|
| 178 |
+
genre_classes.append(row[1])
|
| 179 |
+
# print(genre_classes)
|
| 180 |
+
|
| 181 |
+
import gradio as gr
|
| 182 |
+
|
| 183 |
+
def process_image(input_image):
|
| 184 |
+
desired_size =224
|
| 185 |
+
old_size = input_image.size # old_size[0] is in (width, height) format
|
| 186 |
+
ratio = float(desired_size)/max(old_size)
|
| 187 |
+
new_size = tuple([int(x*ratio) for x in old_size])
|
| 188 |
+
|
| 189 |
+
input_image.thumbnail((desired_size, desired_size), Image.ANTIALIAS)
|
| 190 |
+
|
| 191 |
+
new_im = Image.new("RGB", (desired_size, desired_size))
|
| 192 |
+
new_im.paste(input_image, ((desired_size-new_size[0])//2,
|
| 193 |
+
(desired_size-new_size[1])//2))
|
| 194 |
+
|
| 195 |
+
# new_im.show()
|
| 196 |
+
np_array = np.array(input_image)
|
| 197 |
+
# print(np_array.shape)
|
| 198 |
+
transposed_np_array = np.transpose(np_array)
|
| 199 |
+
# print(transposed_np_array.shape)
|
| 200 |
+
images_list = []
|
| 201 |
+
images_list.append(transposed_np_array)
|
| 202 |
+
np_input = np.asarray(images_list)
|
| 203 |
+
# print(np_input.shape)
|
| 204 |
+
return model.predict(np_input).flatten()
|
| 205 |
+
|
| 206 |
+
def predict(input_image):
|
| 207 |
+
# print(input_image)
|
| 208 |
+
# img = Image.create(input_image)
|
| 209 |
+
pil_image_object = Image.fromarray(input_image)
|
| 210 |
+
probs = process_image(pil_image_object)
|
| 211 |
+
return {genre_classes[i]: float(probs[i]) for i in range(len(genre_classes)-1)}
|
| 212 |
+
|
| 213 |
+
image_path_prefx = os.path.join(ROOT_FOLDER,'examples')
|
| 214 |
+
examples = [f"{image_path_prefx}/e{n}.jpeg" for n in range(4)]
|
| 215 |
+
interpretation='shap'
|
| 216 |
+
title = "MP Art Classifier"
|
| 217 |
+
description = "<b>Classifies Art into 10 Genres</b>"
|
| 218 |
+
theme = 'grass'
|
| 219 |
+
|
| 220 |
+
gr.Interface(
|
| 221 |
+
fn=predict,
|
| 222 |
+
inputs=gr.inputs.Image(shape=((512,512))),
|
| 223 |
+
outputs=gr.outputs.Label(num_top_classes=5),
|
| 224 |
+
title = title,
|
| 225 |
+
examples = examples,
|
| 226 |
+
theme = theme,
|
| 227 |
+
interpretation = interpretation,
|
| 228 |
+
description = description
|
| 229 |
+
).launch(share=True, debug=True)
|
| 230 |
+
|
| 231 |
+
clean_directories()
|
examples/{e4.jpeg → e0.jpeg}
RENAMED
|
File without changes
|