Upload cat&dog.py with huggingface_hub
Browse files- cat&dog.py +96 -0
cat&dog.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Cat&Dogs.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colaboratory.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/17AFfKN67SFvxF7FdjjugeJGIa000SGU8
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
|
| 12 |
+
#load the datasets
|
| 13 |
+
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
|
| 14 |
+
|
| 15 |
+
#pre-process the data
|
| 16 |
+
x_train = tf.keras.utils.normalize(x_train, axis=1)
|
| 17 |
+
x_test = tf.keras.utils.normalize(x_test, axis=1)
|
| 18 |
+
|
| 19 |
+
#define the model input and set the layers
|
| 20 |
+
model = tf.keras.models.Sequential()
|
| 21 |
+
model.add(tf.keras.layers.Flatten())
|
| 22 |
+
model.add(tf.keras.layers.Dense(128, activation=tf.nn.leaky_relu))
|
| 23 |
+
model.add(tf.keras.layers.Dense(128, activation=tf.nn.leaky_relu))
|
| 24 |
+
model.add(tf.keras.layers.Dense(10, activation=tf.nn.sigmoid))
|
| 25 |
+
|
| 26 |
+
#compile the model
|
| 27 |
+
model.compile(optimizer='adam',
|
| 28 |
+
loss='sparse_categorical_crossentropy',
|
| 29 |
+
metrics=['accuracy'])
|
| 30 |
+
|
| 31 |
+
#train the model
|
| 32 |
+
model.fit(x_train, y_train, epochs=100)
|
| 33 |
+
|
| 34 |
+
#evaluate the model
|
| 35 |
+
val_loss, val_acc = model.evaluate(x_test, y_test)
|
| 36 |
+
print(val_loss)
|
| 37 |
+
print(val_acc)
|
| 38 |
+
|
| 39 |
+
#make predictions
|
| 40 |
+
predictions = model.predict(x_test)
|
| 41 |
+
|
| 42 |
+
import matplotlib.pyplot as plt
|
| 43 |
+
import numpy as np
|
| 44 |
+
|
| 45 |
+
# Select 5 random images from the test set
|
| 46 |
+
indices = np.random.randint(0, len(x_test), size=1)
|
| 47 |
+
images = x_test[indices]
|
| 48 |
+
|
| 49 |
+
# Make predictions for the selected images
|
| 50 |
+
predictions = model.predict(images)
|
| 51 |
+
|
| 52 |
+
# Iterate over the images and predictions
|
| 53 |
+
for i, (image, prediction) in enumerate(zip(images, predictions)):
|
| 54 |
+
# Convert the image to uint8 and reshape it to (32, 32, 3)
|
| 55 |
+
image = np.uint8(image * 255).reshape(32, 32, 3)
|
| 56 |
+
|
| 57 |
+
# Get the class label and probability
|
| 58 |
+
label = np.argmax(prediction)
|
| 59 |
+
probability = prediction[label]
|
| 60 |
+
|
| 61 |
+
# Plot the image and the prediction
|
| 62 |
+
plt.subplot(1, 5, i + 1)
|
| 63 |
+
plt.imshow(image)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# The labels of the CIFAR-10 dataset are represented as integers in the range 0 to 9. Each integer corresponds to a class of image:
|
| 67 |
+
|
| 68 |
+
# 0: airplane
|
| 69 |
+
# 1: automobile
|
| 70 |
+
# 2: bird
|
| 71 |
+
# 3: cat
|
| 72 |
+
# 4: deer
|
| 73 |
+
# 5: dog
|
| 74 |
+
# 6: frog
|
| 75 |
+
# 7: horse
|
| 76 |
+
# 8: ship
|
| 77 |
+
# 9: truck
|
| 78 |
+
|
| 79 |
+
plt.title("Prediction: {} ({:.2f})".format(label, probability))
|
| 80 |
+
|
| 81 |
+
plt.show()
|
| 82 |
+
|
| 83 |
+
from huggingface_hub import notebook_login
|
| 84 |
+
notebook_login()
|
| 85 |
+
|
| 86 |
+
from huggingface_hub import create_repo
|
| 87 |
+
create_repo(repo_id="test-model")
|
| 88 |
+
'https://huggingface.co/zegoop/myModel1'
|
| 89 |
+
|
| 90 |
+
from huggingface_hub import upload_file
|
| 91 |
+
upload_file(
|
| 92 |
+
path_or_fileobj="Cat&Dogs.ipynb",
|
| 93 |
+
path_in_repo="cat&dog.ipynb",
|
| 94 |
+
repo_id="zegoop/test-model"
|
| 95 |
+
)
|
| 96 |
+
'https://huggingface.co/zegoop/myModel1/blob/main/cat&dog.ipynb'
|