Delete hf_present_bit_fined_grainedclassification.py
Browse files
hf_present_bit_fined_grainedclassification.py
DELETED
@@ -1,131 +0,0 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
"""HF-Present_BiT_Fined_grainedClassification.ipynb
|
3 |
-
|
4 |
-
Automatically generated by Colaboratory.
|
5 |
-
|
6 |
-
Original file is located at
|
7 |
-
https://colab.research.google.com/drive/11qlOL9TSLiX4a-D6rwkxw7sp2zyUHCE9
|
8 |
-
|
9 |
-
# IMPORT LIBRARIES AND DATASET
|
10 |
-
"""
|
11 |
-
|
12 |
-
#@title Libraries
|
13 |
-
import tensorflow as tf
|
14 |
-
import tensorflow_hub as hub
|
15 |
-
|
16 |
-
import tensorflow_datasets as tfds
|
17 |
-
|
18 |
-
import time
|
19 |
-
|
20 |
-
from PIL import Image
|
21 |
-
import requests
|
22 |
-
from io import BytesIO
|
23 |
-
|
24 |
-
import matplotlib.pyplot as plt
|
25 |
-
import numpy as np
|
26 |
-
|
27 |
-
import os
|
28 |
-
import pathlib
|
29 |
-
|
30 |
-
#@title Gradio
|
31 |
-
!pip install gradio --quiet
|
32 |
-
import gradio as gr
|
33 |
-
|
34 |
-
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
|
35 |
-
from keras.preprocessing import image
|
36 |
-
import tensorflow as tf
|
37 |
-
|
38 |
-
#@title Dùng TensorFlow Datasets để load bộ data oxford_flowers102
|
39 |
-
import tensorflow_datasets as tfds
|
40 |
-
|
41 |
-
(train, val, test), info = tfds.load('oxford_flowers102',
|
42 |
-
split=['train', 'validation', 'test'],
|
43 |
-
shuffle_files=True,
|
44 |
-
as_supervised=True,
|
45 |
-
with_info=True)
|
46 |
-
NUM_CLASSES = 102
|
47 |
-
|
48 |
-
#@title Lưu danh sách tên vào file txt
|
49 |
-
f = open("flower_names.txt", "w")
|
50 |
-
f.write(info.features['label'].names[0])
|
51 |
-
|
52 |
-
i=1
|
53 |
-
while i<102:
|
54 |
-
f.write(" \n")
|
55 |
-
f.write(info.features['label'].names[i])
|
56 |
-
i+=1
|
57 |
-
f.close()
|
58 |
-
|
59 |
-
from google.colab import drive
|
60 |
-
drive.mount('/content/drive')
|
61 |
-
|
62 |
-
"""# Data_Preprocessing
|
63 |
-
|
64 |
-
"""
|
65 |
-
|
66 |
-
from tensorflow import cast, float32
|
67 |
-
from tensorflow.data.experimental import AUTOTUNE
|
68 |
-
from tensorflow import one_hot
|
69 |
-
from tensorflow.image import resize
|
70 |
-
|
71 |
-
def preprocess_data(image, label):
|
72 |
-
"""
|
73 |
-
Normalizes images: `uint8` -> `float32`.
|
74 |
-
One hot encoding labels
|
75 |
-
Resize to (224, 224)
|
76 |
-
"""
|
77 |
-
|
78 |
-
|
79 |
-
return resize(cast(image, float32)/255. , [224, 224]), one_hot(label, 102)
|
80 |
-
|
81 |
-
"""# GRADIO"""
|
82 |
-
|
83 |
-
# Load a previously trained model for gradio demonstration
|
84 |
-
model2=tf.keras.models.load_model('/content/drive/MyDrive/Colab Notebooks/BiT fine_grained-17Jun-20220617T065450Z-001/BiT fine_grained-17Jun')
|
85 |
-
|
86 |
-
model2.summary()
|
87 |
-
|
88 |
-
with open('/content/flower_names.txt') as f:
|
89 |
-
labels = f.readlines()
|
90 |
-
|
91 |
-
from numpy import exp
|
92 |
-
def softmax(vector):
|
93 |
-
e = exp(vector)
|
94 |
-
return e / e.sum()
|
95 |
-
|
96 |
-
def image_to_output (input_img):
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
gr_img=[]
|
102 |
-
gr_img.append(input_img)
|
103 |
-
img2=resize(cast(gr_img, float32)/255. , [224, 224])
|
104 |
-
|
105 |
-
#print(img2)
|
106 |
-
|
107 |
-
x_test=np.asarray(img2)
|
108 |
-
|
109 |
-
prediction = model2.predict(x_test,batch_size=1).flatten()
|
110 |
-
prediction=softmax(prediction)
|
111 |
-
|
112 |
-
confidences = {labels[i]: float(prediction[i]) for i in range(102)}
|
113 |
-
# confidences = {labels[i]:float(top[i]) for i in range(num_predictions)}
|
114 |
-
|
115 |
-
return confidences
|
116 |
-
|
117 |
-
import gradio as gr
|
118 |
-
|
119 |
-
gr.Interface(fn=image_to_output,
|
120 |
-
inputs=gr.inputs.Image(shape=(224,224)),
|
121 |
-
outputs=gr.outputs.Label(num_top_classes=5),
|
122 |
-
interpretation="default"
|
123 |
-
).launch()
|
124 |
-
|
125 |
-
"""## Acknowledgements
|
126 |
-
|
127 |
-
This colab is based on
|
128 |
-
|
129 |
-
BigTransfer (BiT): A step-by-step tutorial for state-of-the-art vision
|
130 |
-
By Jessica Yung and Joan Puigcerver
|
131 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|