# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/sungheeje/tensorflow/blob/master/Untitled1.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + colab={"base_uri": "https://localhost:8080/"} id="Wp1un6KrkCtI" outputId="356f5c15-95a0-460d-94b7-c1c6486a3396"
# tensorflow와 tf.keras를 임포트합니다
import tensorflow as tf
from tensorflow import keras

# 헬퍼(helper) 라이브러리를 임포트합니다
import numpy as np
import matplotlib.pyplot as plt

print(tf.__version__)

# + colab={"base_uri": "https://localhost:8080/"} id="a2uT7t5Fmy1c" outputId="ae12bc2f-692c-4cd7-9ee2-94b632c292cb"
print("Hello world!")

# + colab={"base_uri": "https://localhost:8080/"} id="xb2PZHp9n0Ir" outputId="b66253bb-6e20-41b9-8950-e569d286a691"
fashion_mnist = keras.datasets.fashion_mnist

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# + id="2C5i118ErKG7"
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

# + colab={"base_uri": "https://localhost:8080/"} id="fi-3UKtprOby" outputId="0d0f4f61-bc84-4a0c-bc5c-a71391befc9b"
train_images.shape

# + colab={"base_uri": "https://localhost:8080/"} id="N8a_0UYerbUj" outputId="3d6e3436-d079-4276-b6be-46b84f31561a"
len(train_labels)

# + colab={"base_uri": "https://localhost:8080/"} id="XSr8Z-u4rk2y" outputId="8d5c6688-5bef-46c8-fc64-b10fdf2876b2"
train_labels

# + colab={"base_uri": "https://localhost:8080/"} id="XMHVmHwUr1PS" outputId="c848e418-17e5-4e11-f9a4-a92ed6a6efc1"
test_images.shape

# + colab={"base_uri": "https://localhost:8080/"} id="37hMMQYksJmi" outputId="3f78e13b-4e89-4a35-88a3-03833703dd9b"
len(test_labels)

# + colab={"base_uri": "https://localhost:8080/", "height": 264} id="dvaXzFIGsXCa" outputId="65191f3d-09db-45b3-94c7-61a4ec965e5d"
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()

# + id="NqYZOIp_st6S"
train_images = train_images / 255.0

test_images = test_images / 255.0

# + colab={"base_uri": "https://localhost:8080/", "height": 588} id="c3NaE5Brsx4B" outputId="d05eadb3-9594-4e6c-8fbe-a514703054e9"
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
plt.show()

# + id="Hjmftl5XtaPj"
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

# + id="tCtdwQt5uMjj"
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# + colab={"base_uri": "https://localhost:8080/"} id="Pu1beuCPvRLL" outputId="055828b5-a810-4c6d-f9c6-a1208890f4df"
model.fit(train_images, train_labels, epochs=5)

# + colab={"base_uri": "https://localhost:8080/"} id="W1H22PyiveB_" outputId="dd60ac17-0f19-475d-a100-1dedb22c5e2d"
test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)

print('\n테스트 정확도:', test_acc)

# + id="dcj690iKxBSa"
predictions = model.predict(test_images)

# + colab={"base_uri": "https://localhost:8080/"} id="unlSzRoQxD0b" outputId="1b14de5a-4a40-417d-f458-014d3d8e1ca1"
predictions[0]

# + colab={"base_uri": "https://localhost:8080/"} id="Ys35KcH2xGaT" outputId="aa2959a4-f3b1-4602-a129-00ad3c93f2a1"
np.argmax(predictions[0])

# + colab={"base_uri": "https://localhost:8080/"} id="j2xxadNGxlO6" outputId="c6895cf3-7151-4386-efcd-d5ac3c49a414"
test_labels[0]


# + id="ZIdMjEPEx4r4"
def plot_image(i, predictions_array, true_label, img):
  predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])

  plt.imshow(img, cmap=plt.cm.binary)

  predicted_label = np.argmax(predictions_array)
  if predicted_label == true_label:
    color = 'blue'
  else:
    color = 'red'

  plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
                                100*np.max(predictions_array),
                                class_names[true_label]),
                                color=color)

def plot_value_array(i, predictions_array, true_label):
  predictions_array, true_label = predictions_array[i], true_label[i]
  plt.grid(False)
  plt.xticks([])
  plt.yticks([])
  thisplot = plt.bar(range(10), predictions_array, color="#777777")
  plt.ylim([0, 1])
  predicted_label = np.argmax(predictions_array)

  thisplot[predicted_label].set_color('red')
  thisplot[true_label].set_color('blue')


# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="j4f9jneSx6P6" outputId="63a72ec9-1b46-4ca0-b8fb-fd30ff795849"
i = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 203} id="qDlzH86Rx77C" outputId="39f8a9a9-e526-4ba4-b1ad-eddaa84b8ee8"
i = 12
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(i, predictions, test_labels, test_images)
plt.subplot(1,2,2)
plot_value_array(i, predictions,  test_labels)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 538} id="QrZKFASux98K" outputId="d2008a89-5e5f-41c7-f022-9aec4d990444"
# 처음 X 개의 테스트 이미지와 예측 레이블, 진짜 레이블을 출력합니다
# 올바른 예측은 파랑색으로 잘못된 예측은 빨강색으로 나타냅니다
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
  plt.subplot(num_rows, 2*num_cols, 2*i+1)
  plot_image(i, predictions, test_labels, test_images)
  plt.subplot(num_rows, 2*num_cols, 2*i+2)
  plot_value_array(i, predictions, test_labels)
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="KNbB1Ci2yPmq" outputId="06ac126e-83d5-4679-f4a6-d1b8c047a245"
# 테스트 세트에서 이미지 하나를 선택합니다
img = test_images[0]

print(img.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="Wn9jJw2fySkk" outputId="c55e890f-8e30-4c30-ce2e-141d1beaa5a8"
# 이미지 하나만 사용할 때도 배치에 추가합니다
img = (np.expand_dims(img,0))

print(img.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="5U51fEJXyVL6" outputId="bdafdcdf-7057-4d45-ce88-03b04f18aec8"
predictions_single = model.predict(img)

print(predictions_single)

# + colab={"base_uri": "https://localhost:8080/", "height": 300} id="_OEFO9P_yXGu" outputId="4fa178a9-5417-47fd-9a05-3d7f4fbbffe3"
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

# + id="LZrujFLOyagj"
# MIT License
#
# Copyright (c) 2017 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# # copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# + colab={"base_uri": "https://localhost:8080/"} id="3kU6gefICTBc" outputId="f07fa35c-2bc1-4e62-dd91-9984d8dbb320"
from google.colab import drive
drive.mount('/content/drive')

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="z5XScG-EDcJd" outputId="aa4fc2a6-78e4-48fc-93b8-021a4b8d1850"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/Screenshot_20210528-120608_Samsung Internet.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()


# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="WJTnBtWME_cV" outputId="cba47630-0bfe-4166-e608-acdd1fec6eef"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/outer1.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="Ecu9rVdNFIVk" outputId="f57ea675-c663-4d2a-b785-06e00bbe4a1a"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/outer2.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="tWsWw0wfFK28" outputId="b3b4049f-cb05-49be-8169-5ae477c211e3"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/outer3.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="_FA9vQEQFNKM" outputId="5ef8d6fb-4e18-47c3-bf8a-7e97e85ec1b7"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/shose1.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="ZADFh5r-FV-z" outputId="16ed2a2f-7f09-47c1-d9b4-21f0b5029245"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/shose2.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="vEfCrAMzFZs6" outputId="0d913a4e-a20c-4d1d-cd8f-18f5fcd7ea29"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/shose3.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="6oKVuhTwFcUi" outputId="d1cd99c8-c68c-4379-f8ff-f8c990253d9a"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/watch1.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 268} id="kLdJbgtAFz-D" outputId="fdcf9039-c14e-4982-db2c-ac15e272e865"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/shose4.jpg")
img_cvt=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img_cvt)
plt.show()

# + colab={"base_uri": "https://localhost:8080/", "height": 264} id="MdC8xIvJF3bj" outputId="1a4d73dd-65bc-40fe-c6e9-c40cd567c9df"
import cv2
import numpy as np
from matplotlib import pyplot as pl


img = cv2.imread("/content/drive/MyDrive/shose4.jpg")
img=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, dsize=(28, 28), interpolation=cv2.INTER_AREA)
plt.imshow(img)
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="O7z4SU6cHSM0" outputId="a4af4889-179e-4080-98b1-983d4aa26038"
img.shape

# + id="F5QhCZ-SHwkM"
img = img/255.

# + colab={"base_uri": "https://localhost:8080/", "height": 264} id="3yWwC7xCJRN9" outputId="bb06c093-9887-4c98-a977-9045105256b8"
plt.imshow(img, cmap='Greys_r')
plt.show()

# + colab={"base_uri": "https://localhost:8080/"} id="K7qOKh5nJT8k" outputId="3f83947c-44d8-4ac6-a5a7-aef4ededa0a8"
img

# + colab={"base_uri": "https://localhost:8080/"} id="Mhn2VdXhJYbc" outputId="17dd1490-429c-4578-cfd8-e859c930f786"
img.shape

# + colab={"base_uri": "https://localhost:8080/"} id="7Neo-3lALHUF" outputId="6432cdee-8b8f-4c28-e080-77d17492c4a1"
# 이미지 하나만 사용할 때도 배치에 추가합니다
img = (np.expand_dims(img,0))

print(img.shape)

# + colab={"base_uri": "https://localhost:8080/"} id="AZiwmiAdLLGJ" outputId="5836797b-07ec-4748-d5f3-8999af4ab26e"
predictions_single = model.predict(img)

print(predictions_single)

# + colab={"base_uri": "https://localhost:8080/", "height": 300} id="oqLYEchMLM1E" outputId="2278bc4e-ccbe-44e5-f908-20057de6500e"
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

# + colab={"base_uri": "https://localhost:8080/", "height": 547} id="3Y5sexmYRH_U" outputId="88e69cb5-9a86-4f35-d657-b1620a9f49ef"
import cv2
import numpy as np
from matplotlib import pyplot as plt
fname='t2_negative.png'
img = cv2.imread("/content/drive/MyDrive/shose5.jpg")
img =cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, dsize=(28, 28), interpolation=cv2.INTER_AREA)
img = img/255.
plt.imshow(img)
plt.show()
img = (np.expand_dims(img,0))
predictions_single = model.predict(img)
plot_value_array(0, predictions_single, test_labels)
_ = plt.xticks(range(10), class_names, rotation=45)

StarcoderdataJupyterScriptsDedupFiltered