<jupyter_start><jupyter_text>Load flowers dataset<jupyter_code>dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url,  cache_dir='.', untar=True)
data_dir<jupyter_output><empty_output><jupyter_text>Need to figure how it works<jupyter_code>import pathlib
data_dir = pathlib.Path(data_dir)
data_dir
list(data_dir.glob('*/*.jpg'))[:5]
image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)
roses = list(data_dir.glob('roses/*'))
roses[:5]
Image.open(str(roses[1]))
tulips = list(data_dir.glob('tulips/*'))
Image.open(tulips[0])
# Image.open(str(tulips[0]))
flowers_images_dict = {
    'roses': list(data_dir.glob('roses/*')),
    'daisy': list(data_dir.glob('daisy/*')),
    'dandelion': list(data_dir.glob('dandelion/*')),
    'sunflowers': list(data_dir.glob('sunflowers/*')),
    'tulips': list(data_dir.glob('tulips/*')),
}<jupyter_output><empty_output><jupyter_text>Remember how useful dictionary is<jupyter_code>flowers_labels_dict = {
    'roses': 0,
    'daisy': 1,
    'dandelion': 2,
    'sunflowers': 3,
    'tulips': 4,
}
Image.open(str(flowers_images_dict['roses'][0]))
img = cv2.imread(str(flowers_images_dict['roses'][0]))
print(flowers_images_dict['roses'][0])
img.shape
cv2.resize(img,(224,224)).shape
X, y = [], []

for flower_name, images in flowers_images_dict.items():
    for image in images:
        img = cv2.imread(str(image))
        resized_img = cv2.resize(img,(224,224))
        X.append(resized_img)
        y.append(flowers_labels_dict[flower_name])

X = np.array(X)
y = np.array(y)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# Preprocessing: scale images
X_train_scaled = X_train / 255
X_test_scaled = X_test / 255
plt.axis('off')
plt.imshow(X[0])
print(X[0].shape)
print(IMAGE_SHAPE+(3,))
print(type(X[0]), X[0].shape)
x0_resized = cv2.resize(X[0], IMAGE_SHAPE)
x1_resized = cv2.resize(X[1], IMAGE_SHAPE)
x2_resized = cv2.resize(X[2], IMAGE_SHAPE)
print(type(x0_resized))
if IMAGE_SHAPE+(3,) == X[0].shape:
  print("Yes")
if IMAGE_SHAPE+(3,) == x0_resized.shape:
  print("Yes")

print(type(X[0]))
print("Change")
print(type(np.array(X[0])))
predicted = classifier.predict(np.array([X[0], X[1], X[2]])) # predicted = classifier.predict(np.array([X[0], X[1], X[2]]))
predicted = np.argmax(predicted, axis=1)
predicted
for index in predicted:
  print("Predicted: ", image_labels[index])<jupyter_output>Predicted:  shower curtain
Predicted:  envelope
Predicted:  pillow<jupyter_text>Using pretrained model and retrain it using flower images<jupyter_code>feature_extractor_model = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
pretrained_model_without_top_layer = hub.KerasLayer(
    feature_extractor_model, input_shape=(224, 224, 3), trainable=False)

num_of_flowers = 5

model = tf.keras.Sequential([
  pretrained_model_without_top_layer,
  tf.keras.layers.Dense(num_of_flowers, activation='softmax')
])

model.summary()

model.compile(
  optimizer="adam",
  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
  metrics=['acc'])

model.fit(X_train_scaled, y_train, epochs=5)
model.evaluate(X_test_scaled, y_test)<jupyter_output>23/23 [==============================] - 3s 110ms/step - loss: 0.3896 - acc: 0.8651
{'fraction_non_alphanumeric': 0.1762295081967213, 'fraction_numerical': 0.02810304449648712, 'mean_word_length': 6.661434977578475, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '530367', 'license': 'MIT', 'n_tokens_mistral': 1417, 'n_tokens_neox': 1350, 'n_words': 195, 'path': 'Transfer_Learning_Using_Pretrained_Model.ipynb', 'repo_name': 'Hacker-Davinci/Tools_And_Basics_For_Mahcine_Learning_And_Data_Science', 'size': '3416'}