Part3-12 tf.data - fit with tf.data
fit.generator로 넣는거 진행
def get_class_name(path):
fname = tf.strings.split(path,'_')[-1]
lbl_name = tf.strings.regex_replace(fname, '.png','')
return lbl_name
#어제 강의과 동일하게 onehot_encoding은 안됨, 진행은 되기 때문에 일단 내버려둠
class_names = [get_class_name(path) for path in train_paths]
classes = tf.unique(class_names).y.numpy()
def onehot_encoding(label_name):
onehot_encoding = tf.cast(classes == get_class_name(path),tf.uint8)
return onehot_encoding
def read_dataset(path):
#read image
gfile = tf.io.read_file(path)
image = tf.io.decode_image(gfile)
image = tf.cast(image,tf.float32)/255
# read label
class_name = get_class_name(path)
label = onehot_encoding(class_name)
return image, label
train_dataset = tf.data.Dataset.from_tensor_slices(train_paths)
train_dataset = train_dataset.map(read_dataset)
train_dataset = train_dataset.map(image_preprocess)
train_dataset = train_dataset.batch(batch_size)
train_dataset = train_dataset.shuffle(buffer_size=len(train_paths))
train_dataset = train_dataset.repeat()
test_dataset = tf.data.Dataset.from_tensor_slices(test_paths)
test_dataset = test_dataset.map(read_dataset)
test_dataset = test_dataset.batch(batch_size)
test_dataset = test_dataset.shuffle(buffer_size=len(test_paths))
test_dataset = test_dataset.repeat()

transform image
def image_preprocess(image, label):
image = tf.image.random_flip_up_down(image)
image = tf.image.random_flip_left_right(image)
return image, label
transformed , label = image_preprocess(image, label)
plt.subplot(121)
plt.imshow(image)
plt.subplot(122)
plt.imshow(image_preprocessed)
plt.show()

training
직접 넣어줘야함
steps_per_epoch = len(train_paths) // batch_size
validation_steps = len(test_paths) // batch_size
model.fit_generator(
train_dataset,
steps_per_epoch=steps_per_epoch,
validation_data=test_dataset,
validation_steps=validation_steps,
epochs=num_epochs
)
Part3-13 callbacks - tensorboard1
callbacks 학습도중에 이벤트를 일으키는 것
checkpoint 저장 등 커스텀해서 만들수 있음
매직명령어로 tensorboard 바로 볼수 있게 가능
%load_ext tensorboard
데이터 1000개만 확인
callbacks
처음에 정해야하는 것은 어디다 저장할지 것인지
logdir = os.path.join('logs', datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard = tf.keras.callbacks.TensorBoard(
log_dir=logdir,
write_graph=True,
write_images=True,
histogram_freq=1
)
에러나는경우 포트변경해서 진행
%tensorboard --logdir logs --port 8008

LambdaCallback
- 언제 실행시킬것인지 정할 수 있음
- log_confusion_matrix 에폭이 끝날때 마다 실행
- confuion_matrix를 sklearn으로 그림
- tf.summary.image에 그림을 담음
- log_confusion_matrix로 함수화 시켜 전달
confuion_matrix를 그리는 함수
import sklearn.metrics
import itertools
import io
file_writer_cm = tf.summary.create_file_writer(logdir + '/cm')
def plot_to_image(figure):
"""Converts the matplotlib plot specified by 'figure' to a PNG image and
returns it. The supplied figure is closed and inaccessible after this call."""
# Save the plot to a PNG in memory.
buf = io.BytesIO()
plt.savefig(buf, format='png')
# Closing the figure prevents it from being displayed directly inside
# the notebook.
plt.close(figure)
buf.seek(0)
# Convert PNG buffer to TF image
image = tf.image.decode_png(buf.getvalue(), channels=4)
# Add the batch dimension
image = tf.expand_dims(image, 0)
return image
def plot_confusion_matrix(cm, class_names):
"""
Returns a matplotlib figure containing the plotted confusion matrix.
Args:
cm (array, shape = [n, n]): a confusion matrix of integer classes
class_names (array, shape = [n]): String names of the integer classes
"""
figure = plt.figure(figsize=(8, 8))
plt.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)
plt.title("Confusion matrix")
plt.colorbar()
tick_marks = np.arange(len(class_names))
plt.xticks(tick_marks, class_names, rotation=45)
plt.yticks(tick_marks, class_names)
# Normalize the confusion matrix.
cm = np.around(cm.astype('float') / cm.sum(axis=1)[:, np.newaxis], decimals=2)
# Use white text if squares are dark; otherwise black.
threshold = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
color = "white" if cm[i, j] > threshold else "black"
plt.text(j, i, cm[i, j], horizontalalignment="center", color=color)
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
return figure
tf.summary.image에 그림을 담은 것을 함수화
def log_confusion_matrix(epoch, logs):
# Use the model to predict the values from the validation dataset.
test_pred_raw = model.predict(test_images)
test_pred = np.argmax(test_pred_raw, axis=1)
# Calculate the confusion matrix.
cm = sklearn.metrics.confusion_matrix(test_labels, test_pred)
# Log the confusion matrix as an image summary.
figure = plot_confusion_matrix(cm, class_names=class_names)
cm_image = plot_to_image(figure)
# Log the confusion matrix as an image summary.
with file_writer_cm.as_default():
tf.summary.image("Confusion Matrix", cm_image, step=epoch)
에폭이 끝날때마다 callback실행
# Define the per-epoch callback.
cm_callback = tf.keras.callbacks.LambdaCallback(on_epoch_end=log_confusion_matrix)
'책&강의 학습 > 올인원 패키지 :딥러닝&인공지능' 카테고리의 다른 글
| [패스트캠퍼스 수강 후기] 인공지능강의 100% 환급 챌린지 16회차 미션 (0) | 2020.07.14 |
|---|---|
| [패스트캠퍼스 수강 후기] 인공지능강의 100% 환급 챌린지 15회차 미션 (0) | 2020.07.13 |
| [패스트캠퍼스 수강 후기] 인공지능강의 100% 환급 챌린지 13회차 미션 (0) | 2020.07.11 |
| [패스트캠퍼스 수강 후기] 인공지능강의 100% 환급 챌린지 12회차 미션 (0) | 2020.07.10 |
| [패스트캠퍼스 수강 후기] 인공지능강의 100% 환급 챌린지 11회차 미션 (0) | 2020.07.09 |
댓글