minhui study

tensorflow 2.0 - post process & save and load model 본문

딥러닝,인공지능

tensorflow 2.0 - post process & save and load model

minhui 2021. 1. 27. 21:48

post_process_history

TensorFlow 2.0 → Hyperparameter Tunning → Build Model → Data Preprocess Training

 

History 들여다 보기

history.history.keys()

history.params

mew_model = history.model
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title("Model Accuracy")
plt.ylabel("accuracy")
plt.xlabel("epoch")
plt.legend(['train', 'validation'])
plt.show

 

 

 

post_process_predict&predict_generator

TensorFlow 2.0 → Hyperparameter Tunning → Build Model → Data Preprocess  → Training

 

이미지를 직접 load해서 넣는 방법

path = train_paths[0]
test_image, test_label = load_image_label(path)
test_image.shape

test_image = test_image[tf.newaxis, ...]
test_image.shape

pred = model.predict(test_image)
pred

 

 

 

generator에서 데이터를 가져오는 방법

test_image, test_label = next(iter(test_dataset))
test_image.shape

pred = model.predict(test_image)
pred.shape

pred[0]

 

 

 

generator에 넣는 방법

pred = model.predict_generator(test_dataset.take(1)) #배치 한개만 가져온다.
pred.shape

pred = model.predict_generator(test_dataset.take(2)) # 2개를 가져온다.
pred.shape

 

 

save and load model

 

TensorFlow 2.0 → Hyperparameter Tunning → Build Model → Data Preprocess  → (Checkpoint) Training

 

Saving Model

save_path = 'my_model.h5'
model.save(save_path, include_optimizer=True)
model = tf.keras.models.load_model('my_model.h5') #모델을 불러온다.

Saving Model - 2

# Save the weights
model.save_weights('model_weights.h5') #모델의 weight만 저장한다.

# Save the model architecture
with open('model_architecture.json', 'w') as f:
    f.write(model.to_json())
    
from tensorflow.keras.models import model_from_json

# Model reconstruction from JSON file
with open('model_architecture.json', 'r') as f:
    model = model_from_json(f.read())

# Load weights into the new model
model.load_weights('model_weights.h5')

#model.load_weights('checkpoints')이런 식으로 checkpoint가 담긴 폴더를 지정해주면 checkpoint도 불러올 수 있다.

 

 

model.h5 들여다보기

import h5py
model_file = h5py.File('my_model.h5','r+')
model_file.keys()

model_file['model_weights'].keys()

model_file['model_weights']['conv2d']['conv2d'].keys()

model_file['model_weights']['conv2d']['conv2d']['kernel:0']

np.array(model_file['model_weights']['conv2d']['conv2d']['kernel:0']) #weight꺼내오기

Comments