Spaces:
Runtime error
Runtime error
jaekookang
commited on
Commit
Β·
39a6dd6
1
Parent(s):
af72b72
test app
Browse files- .gitignore +2 -0
- .ipynb_checkpoints/gradio_artist_classifier-checkpoint.py +52 -4
- .ipynb_checkpoints/requirements-checkpoint.txt +4 -0
- artist.json +12 -0
- gradio_artist_classifier.py +52 -4
- monet.jpg +0 -0
- requirements-dev.txt +9 -0
- requirements.txt +4 -0
- trend.json +10 -0
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.ipynb_checkpoints
|
| 2 |
+
*~
|
.ipynb_checkpoints/gradio_artist_classifier-checkpoint.py
CHANGED
|
@@ -10,14 +10,62 @@ import matplotlib.pyplot as plt
|
|
| 10 |
import matplotlib.image as mpimg
|
| 11 |
import seaborn as sns
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
import gradio as gr
|
| 14 |
import tensorflow as tf
|
| 15 |
tfk = tf.keras
|
| 16 |
|
| 17 |
from gradcam_utils import get_img_4d_array, make_gradcam_heatmap, align_image_with_heatmap
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import matplotlib.image as mpimg
|
| 11 |
import seaborn as sns
|
| 12 |
|
| 13 |
+
import json
|
| 14 |
+
import skimage.io
|
| 15 |
+
from loguru import logger
|
| 16 |
+
from huggingface_hub import from_pretrained_keras
|
| 17 |
import gradio as gr
|
| 18 |
import tensorflow as tf
|
| 19 |
tfk = tf.keras
|
| 20 |
|
| 21 |
from gradcam_utils import get_img_4d_array, make_gradcam_heatmap, align_image_with_heatmap
|
| 22 |
|
| 23 |
+
# ---------- Settings ----------
|
| 24 |
+
ARTIST_META = 'artist.json'
|
| 25 |
+
TREND_META = 'trend.json'
|
| 26 |
+
EXAMPLES = ['monet.jpg']
|
| 27 |
|
| 28 |
+
# ---------- Logging ----------
|
| 29 |
+
logger.add('app.log', mode='a')
|
| 30 |
+
logger.info('============================= App restarted =============================')
|
| 31 |
+
|
| 32 |
+
# ---------- Model ----------
|
| 33 |
+
logger.info('loading models...')
|
| 34 |
+
artist_model = from_pretrained_keras("jkang/drawing-artist-classifier")
|
| 35 |
+
trend_model = from_pretrained_keras("jkang/drawing-artistic-trend-classifier")
|
| 36 |
+
logger.info('both models loaded')
|
| 37 |
+
|
| 38 |
+
def load_image_as_array(image_file):
|
| 39 |
+
img = skimage.io.imread(image_file, as_gray=False, plugin='matplotlib')
|
| 40 |
+
if (img.shape[-1] > 3) & (remove_alpha_channel): # if RGBA
|
| 41 |
+
img = img[..., :-1]
|
| 42 |
+
return img
|
| 43 |
+
|
| 44 |
+
def load_image_as_tensor(image_file):
|
| 45 |
+
img = tf.io.read_file(image_file)
|
| 46 |
+
img = tf.io.decode_jpeg(img, channels=3)
|
| 47 |
+
return img
|
| 48 |
+
|
| 49 |
+
def predict(input_image):
|
| 50 |
+
img_3d_array = load_image_as_array(input_image)
|
| 51 |
+
img_4d_tensor = load_image_as_tensor(input_image)
|
| 52 |
+
logger.info(f'--- {input_image} loaded')
|
| 53 |
+
|
| 54 |
+
artist_model(img_4d_tensor);
|
| 55 |
+
trend_model(img_4d_tensor);
|
| 56 |
+
|
| 57 |
+
return img_3d_array
|
| 58 |
+
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
predict,
|
| 61 |
+
title='Predict Artist and Artistic Trend of Drawings π¨π¨π»βπ¨ (prototype)',
|
| 62 |
+
description='Upload a drawing and the model will predict how likely it seems given 10 artists and their trend/style',
|
| 63 |
+
inputs=[
|
| 64 |
+
gr.inputs.Image(label='Upload a drawing/image', type='file')
|
| 65 |
+
],
|
| 66 |
+
outputs=[
|
| 67 |
+
gr.outputs.Image(label='Prediction')
|
| 68 |
+
],
|
| 69 |
+
examples=EXAMPLES,
|
| 70 |
+
)
|
| 71 |
+
iface.launch(debug=True, enable_queue=True)
|
.ipynb_checkpoints/requirements-checkpoint.txt
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
gradio==2.7.0
|
|
|
|
|
|
|
| 2 |
matplotlib==3.5.1
|
| 3 |
numpy==1.22.0
|
|
|
|
| 4 |
seaborn==0.11.2
|
|
|
|
| 5 |
tensorflow==2.7.0
|
|
|
|
| 1 |
gradio==2.7.0
|
| 2 |
+
huggingface_hub==0.4.0
|
| 3 |
+
loguru==0.5.3
|
| 4 |
matplotlib==3.5.1
|
| 5 |
numpy==1.22.0
|
| 6 |
+
scikit_image==0.19.1
|
| 7 |
seaborn==0.11.2
|
| 8 |
+
scikit-image==0.19.1
|
| 9 |
tensorflow==2.7.0
|
artist.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"claude_monet": 0,
|
| 3 |
+
"henri_matisse": 1,
|
| 4 |
+
"jean_michel_basquiat": 2,
|
| 5 |
+
"keith_haring": 3,
|
| 6 |
+
"pablo_picasso": 4,
|
| 7 |
+
"pierre_augste_renoir": 5,
|
| 8 |
+
"rene_magritte": 6,
|
| 9 |
+
"roy_richtenstein": 7,
|
| 10 |
+
"vincent_van_gogh": 8,
|
| 11 |
+
"wassily_kandinsky": 9
|
| 12 |
+
}
|
gradio_artist_classifier.py
CHANGED
|
@@ -10,14 +10,62 @@ import matplotlib.pyplot as plt
|
|
| 10 |
import matplotlib.image as mpimg
|
| 11 |
import seaborn as sns
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
import gradio as gr
|
| 14 |
import tensorflow as tf
|
| 15 |
tfk = tf.keras
|
| 16 |
|
| 17 |
from gradcam_utils import get_img_4d_array, make_gradcam_heatmap, align_image_with_heatmap
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
import matplotlib.image as mpimg
|
| 11 |
import seaborn as sns
|
| 12 |
|
| 13 |
+
import json
|
| 14 |
+
import skimage.io
|
| 15 |
+
from loguru import logger
|
| 16 |
+
from huggingface_hub import from_pretrained_keras
|
| 17 |
import gradio as gr
|
| 18 |
import tensorflow as tf
|
| 19 |
tfk = tf.keras
|
| 20 |
|
| 21 |
from gradcam_utils import get_img_4d_array, make_gradcam_heatmap, align_image_with_heatmap
|
| 22 |
|
| 23 |
+
# ---------- Settings ----------
|
| 24 |
+
ARTIST_META = 'artist.json'
|
| 25 |
+
TREND_META = 'trend.json'
|
| 26 |
+
EXAMPLES = ['monet.jpg']
|
| 27 |
|
| 28 |
+
# ---------- Logging ----------
|
| 29 |
+
logger.add('app.log', mode='a')
|
| 30 |
+
logger.info('============================= App restarted =============================')
|
| 31 |
+
|
| 32 |
+
# ---------- Model ----------
|
| 33 |
+
logger.info('loading models...')
|
| 34 |
+
artist_model = from_pretrained_keras("jkang/drawing-artist-classifier")
|
| 35 |
+
trend_model = from_pretrained_keras("jkang/drawing-artistic-trend-classifier")
|
| 36 |
+
logger.info('both models loaded')
|
| 37 |
+
|
| 38 |
+
def load_image_as_array(image_file):
|
| 39 |
+
img = skimage.io.imread(image_file, as_gray=False, plugin='matplotlib')
|
| 40 |
+
if (img.shape[-1] > 3) & (remove_alpha_channel): # if RGBA
|
| 41 |
+
img = img[..., :-1]
|
| 42 |
+
return img
|
| 43 |
+
|
| 44 |
+
def load_image_as_tensor(image_file):
|
| 45 |
+
img = tf.io.read_file(image_file)
|
| 46 |
+
img = tf.io.decode_jpeg(img, channels=3)
|
| 47 |
+
return img
|
| 48 |
+
|
| 49 |
+
def predict(input_image):
|
| 50 |
+
img_3d_array = load_image_as_array(input_image)
|
| 51 |
+
img_4d_tensor = load_image_as_tensor(input_image)
|
| 52 |
+
logger.info(f'--- {input_image} loaded')
|
| 53 |
+
|
| 54 |
+
artist_model(img_4d_tensor);
|
| 55 |
+
trend_model(img_4d_tensor);
|
| 56 |
+
|
| 57 |
+
return img_3d_array
|
| 58 |
+
|
| 59 |
+
iface = gr.Interface(
|
| 60 |
+
predict,
|
| 61 |
+
title='Predict Artist and Artistic Trend of Drawings π¨π¨π»βπ¨ (prototype)',
|
| 62 |
+
description='Upload a drawing and the model will predict how likely it seems given 10 artists and their trend/style',
|
| 63 |
+
inputs=[
|
| 64 |
+
gr.inputs.Image(label='Upload a drawing/image', type='file')
|
| 65 |
+
],
|
| 66 |
+
outputs=[
|
| 67 |
+
gr.outputs.Image(label='Prediction')
|
| 68 |
+
],
|
| 69 |
+
examples=EXAMPLES,
|
| 70 |
+
)
|
| 71 |
+
iface.launch(debug=True, enable_queue=True)
|
monet.jpg
ADDED
|
requirements-dev.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==2.7.0
|
| 2 |
+
huggingface_hub==0.4.0
|
| 3 |
+
loguru==0.5.3
|
| 4 |
+
matplotlib==3.5.1
|
| 5 |
+
numpy==1.22.0
|
| 6 |
+
scikit_image==0.19.1
|
| 7 |
+
seaborn==0.11.2
|
| 8 |
+
scikit-image==0.19.1
|
| 9 |
+
tensorflow==2.7.0
|
requirements.txt
CHANGED
|
@@ -1,5 +1,9 @@
|
|
| 1 |
gradio==2.7.0
|
|
|
|
|
|
|
| 2 |
matplotlib==3.5.1
|
| 3 |
numpy==1.22.0
|
|
|
|
| 4 |
seaborn==0.11.2
|
|
|
|
| 5 |
tensorflow==2.7.0
|
|
|
|
| 1 |
gradio==2.7.0
|
| 2 |
+
huggingface_hub==0.4.0
|
| 3 |
+
loguru==0.5.3
|
| 4 |
matplotlib==3.5.1
|
| 5 |
numpy==1.22.0
|
| 6 |
+
scikit_image==0.19.1
|
| 7 |
seaborn==0.11.2
|
| 8 |
+
scikit-image==0.19.1
|
| 9 |
tensorflow==2.7.0
|
trend.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cubism": 0,
|
| 3 |
+
"expressionism": 1,
|
| 4 |
+
"fauvisme": 2,
|
| 5 |
+
"graffitiart": 3,
|
| 6 |
+
"impressionism": 4,
|
| 7 |
+
"popart": 5,
|
| 8 |
+
"post_impressionism": 6,
|
| 9 |
+
"surrealism": 7
|
| 10 |
+
}
|