mirror of
https://github.com/tengge1/ShadowEditor.git
synced 2026-01-25 15:08:11 +00:00
intro_to_cnns
This commit is contained in:
parent
69ee0f3dfe
commit
8d58790f31
@ -37,6 +37,7 @@ pip install tensorflow-gpu==2.0.0-rc0
|
||||
8. tensorflow/basic/basic_regression.py: 线性回归
|
||||
9. tensorflow/basic/overfit_and_underfit.py: 过拟合和欠拟合,准确度:99.99%
|
||||
10. tensorflow/basic/save_and_restore_models.py: 保存和恢复模型
|
||||
11. tensorflow/basic/intro_to_cnns.py: 卷积神经网络
|
||||
|
||||
## 相关地址
|
||||
|
||||
|
||||
52
ShadowEditor.AI/tensorflow/basic/hub_with_keras.py
Normal file
52
ShadowEditor.AI/tensorflow/basic/hub_with_keras.py
Normal file
@ -0,0 +1,52 @@
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import matplotlib.pylab as plt
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
import tensorflow_hub as hub
|
||||
|
||||
from tensorflow.keras import layers
|
||||
|
||||
classifier_url ="https://hub.tensorflow.google.cn/google/tf2-preview/mobilenet_v2/classification/2" #@param {type:"string"}
|
||||
|
||||
IMAGE_SHAPE = (224, 224)
|
||||
|
||||
classifier = tf.keras.Sequential([
|
||||
hub.KerasLayer(classifier_url, input_shape=IMAGE_SHAPE+(3,))
|
||||
])
|
||||
|
||||
import numpy as np
|
||||
import PIL.Image as Image
|
||||
|
||||
grace_hopper = tf.keras.utils.get_file('image.jpg','https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg')
|
||||
grace_hopper = Image.open(grace_hopper).resize(IMAGE_SHAPE)
|
||||
|
||||
grace_hopper = np.array(grace_hopper)/255.0
|
||||
|
||||
result = classifier.predict(grace_hopper[np.newaxis, ...])
|
||||
|
||||
predicted_class = np.argmax(result[0], axis=-1)
|
||||
|
||||
labels_path = tf.keras.utils.get_file('ImageNetLabels.txt','https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt')
|
||||
imagenet_labels = np.array(open(labels_path).read().splitlines())
|
||||
|
||||
plt.imshow(grace_hopper)
|
||||
plt.axis('off')
|
||||
predicted_class_name = imagenet_labels[predicted_class]
|
||||
_ = plt.title("Prediction: " + predicted_class_name.title())
|
||||
|
||||
# data_root = tf.keras.utils.get_file(
|
||||
# 'flower_photos','https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
|
||||
# untar=True)
|
||||
|
||||
# image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
|
||||
# image_data = image_generator.flow_from_directory(str(data_root), target_size=IMAGE_SHAPE)
|
||||
|
||||
# for image_batch, label_batch in image_data:
|
||||
# print("Image batch shape: ", image_batch.shape)
|
||||
# print("Label batch shape: ", label_batch.shape)
|
||||
# break
|
||||
|
||||
# result_batch = classifier.predict(image_batch)
|
||||
|
||||
37
ShadowEditor.AI/tensorflow/basic/intro_to_cnns.py
Normal file
37
ShadowEditor.AI/tensorflow/basic/intro_to_cnns.py
Normal file
@ -0,0 +1,37 @@
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import tensorflow as tf
|
||||
|
||||
from tensorflow.keras import datasets, layers, models
|
||||
|
||||
(train_images, train_labels), (test_images,
|
||||
test_labels) = datasets.mnist.load_data()
|
||||
|
||||
train_images = train_images.reshape((60000, 28, 28, 1))
|
||||
test_images = test_images.reshape((10000, 28, 28, 1))
|
||||
|
||||
# Normalize pixel values to be between 0 and 1
|
||||
train_images, test_images = train_images / 255.0, test_images / 255.0
|
||||
|
||||
model = models.Sequential()
|
||||
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
|
||||
model.add(layers.MaxPooling2D((2, 2)))
|
||||
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
|
||||
model.add(layers.MaxPooling2D((2, 2)))
|
||||
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
|
||||
|
||||
model.summary()
|
||||
|
||||
model.add(layers.Flatten())
|
||||
model.add(layers.Dense(64, activation='relu'))
|
||||
model.add(layers.Dense(10, activation='softmax'))
|
||||
|
||||
model.summary()
|
||||
|
||||
model.compile(optimizer='adam',
|
||||
loss='sparse_categorical_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
|
||||
model.fit(train_images, train_labels, epochs=5)
|
||||
|
||||
test_loss, test_acc = model.evaluate(test_images, test_labels)
|
||||
Loading…
x
Reference in New Issue
Block a user