๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿค–๋จธ์‹ ๋Ÿฌ๋‹/ํ…์„œํ”Œ๋กœ์šฐ

ํ…์„œํ”Œ๋กœ์šฐ - ๊ฝƒ ์ด๋ฏธ์ง€ ๋งž์ถ”๊ธฐ

by Janger 2022. 4. 29.
728x90
๋ฐ˜์‘ํ˜•
from shutil import ExecError
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf

from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential

# ๋ฐ์ดํ„ฐ์„ธํŠธ ๋‹ค์šด๋กœ๋“œ ๋ฐ ํƒ์ƒ‰ํ•˜๊ธฐ
import pathlib
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, untar=True)
data_dir = pathlib.Path(data_dir)

print(data_dir)

image_count = len(list(data_dir.glob('*/*.jpg')))
print(image_count)


# ๋ฐ์ดํ„ฐ์„ธํŠธ ๋งŒ๋“ค๊ธฐ

batch_size = 32
img_height = 180
img_width = 180

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)


val_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)
  


# ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ

class_names = train_ds.class_names
print(class_names)

num_classes = 5

model = Sequential([
  layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(32, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Conv2D(64, 3, padding='same', activation='relu'),
  layers.MaxPooling2D(),
  layers.Flatten(),
  layers.Dense(128, activation='relu'),
  layers.Dense(num_classes)
])


# ๋ชจ๋ธ ์ปดํŒŒ์ผํ•˜๊ธฐ

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])


# ๋ชจ๋ธ ํ›ˆ๋ จํ•˜๊ธฐ

epochs=10
history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)



# ๋ฐ์ดํ„ฐ ์ฆ๊ฐ•

data_augmentation = keras.Sequential(
  [
    layers.experimental.preprocessing.RandomFlip("horizontal", 
                                                 input_shape=(img_height, 
                                                              img_width,
                                                              3)),
    layers.experimental.preprocessing.RandomRotation(0.1),
    layers.experimental.preprocessing.RandomZoom(0.1),
  ]
)


# ์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ๋กœ ์˜ˆ์ธกํ•˜๊ธฐ

while True:

    try:
        flower_url = input('์ด๋ฏธ์ง€ URL ์ž…๋ ฅ: ') # "https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg"
        flower_path = tf.keras.utils.get_file('flower', origin=flower_url)

        img = keras.preprocessing.image.load_img(
            flower_path, target_size=(img_height, img_width)
        )
        img_array = keras.preprocessing.image.img_to_array(img)
        img_array = tf.expand_dims(img_array, 0) # Create a batch

        predictions = model.predict(img_array)
        score = tf.nn.softmax(predictions[0])

        print(
            "This image most likely belongs to {} with a {:.2f} percent confidence."
            .format(class_names[np.argmax(score)], 100 * np.max(score))
        )

        os.system('del ' + flower_path)

    except Exception as e:
        print('์˜ˆ์™ธ ์˜ค๋ฅ˜: ', e)

 

๋ฌธ์„œ: 

https://www.tensorflow.org/tutorials/images/classification?hl=ko#%EA%B3%BC%EB%8C%80%EC%A0%81%ED%95%A9 

 

์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜  |  TensorFlow Core

์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์ด ํŠœํ† ๋ฆฌ์–ผ์€ ๊ฝƒ ์ด๋ฏธ์ง€๋ฅผ ๋ถ„๋ฅ˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. keras.Sequential ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜์ž๋ฅผ ๋งŒ๋“ค๊ณ  preprocessing.image_dataset_from_directory๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค.

www.tensorflow.org

 

728x90
๋ฐ˜์‘ํ˜•