<jupyter_start><jupyter_text>Практика применения [ResNet](https://neurohive.io/ru/vidy-nejrosetej/resnet-34-50-101/) [(документация TF.keras)](https://www.tensorflow.org/api_docs/python/tf/keras/applications/resnet).<jupyter_code>import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
import numpy as np
import datetime as dt
[cars_ds_train, cars_ds_test] , cars_info = tfds.load('cars196', split=['train', 'test'], with_info=True)<jupyter_output>[1mDownloading and preparing dataset cars196/2.0.0 (download: 1.82 GiB, generated: Unknown size, total: 1.82 GiB) to /root/tensorflow_datasets/cars196/2.0.0...[0m<jupyter_text>Проверка что датасет [Cars196](https://www.tensorflow.org/datasets/catalog/cars196) загрузился верно.<jupyter_code>fig = tfds.show_examples(cars_info, cars_ds_train)<jupyter_output><empty_output><jupyter_text>Смотрим информацию о датасете.<jupyter_code>cars_info<jupyter_output><empty_output><jupyter_text>Имеем 196 классов и необходимость нормализовать данные.<jupyter_code>def normalize(input_image):
  input_image = tf.cast(input_image, tf.float32) / 255.0
  return input_image
@tf.function
def load_image_train(datapoint):
  input_image = tf.image.resize(datapoint['image'], (224, 224))
  input_label = datapoint['label']

  if tf.random.uniform(()) > 0.5:
    input_image = tf.image.flip_left_right(input_image)

  input_image = normalize(input_image)

  return input_image, input_label
def load_image_test(datapoint):
  input_image = tf.image.resize(datapoint['image'], (224, 224))
  input_label = datapoint['label']
  input_image = normalize(input_image)

  return input_image, input_label
TRAIN_LENGTH = cars_info.splits['train'].num_examples
BATCH_SIZE = 64
BUFFER_SIZE = 1000
STEPS_PER_EPOCH = TRAIN_LENGTH // BATCH_SIZE
list_of_labels = cars_info.features['label'].names
train = cars_ds_train.map(load_image_train, num_parallel_calls=tf.data.experimental.AUTOTUNE)
test = cars_ds_test.map(load_image_test)
train_dataset = train.cache().shuffle(BUFFER_SIZE).batch(BATCH_SIZE).repeat()
train_dataset = train_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)
test_dataset = test.batch(BATCH_SIZE)<jupyter_output><empty_output><jupyter_text>Функция позволяющая посмотреть на отнормированные данные<jupyter_code>def display(display_list, labels_list):
  plt.figure(figsize=(15, 15))

  title = ['Image']

  for i in range(len(display_list)):
    plt.subplot(1, len(display_list), i+1)
    plt.title(list_of_labels[labels_list[i].numpy()])
    plt.imshow(tf.keras.preprocessing.image.array_to_img(display_list[i]))
    plt.axis('off')
  plt.show()
for image, label in train.take(1):
  sample_image, sample_label = image, label
display([sample_image], [sample_label])<jupyter_output><empty_output><jupyter_text>Обучем модель ResNet50. Модели больше большей размерности обочуть на GoogleColab не получается.<jupyter_code>model = tf.keras.applications.ResNet50(
    include_top=True, weights=None, input_shape=(224, 224, 3), classes=196)
#model_101 = tf.keras.applications.ResNet101(
#    include_top=False, weights=None, input_shape=(224, 224, 3), pooling='max', classes=196)
#tf.keras.utils.plot_model(model_101, show_shapes=True)
EPOCHS = 20
VAL_SUBSPLITS = 5
VALIDATION_STEPS = cars_info.splits['test'].num_examples//BATCH_SIZE//VAL_SUBSPLITS
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
print(test_dataset.take(1))
model.fit(train_dataset, epochs=EPOCHS, steps_per_epoch=STEPS_PER_EPOCH)
!mkdir -p saved_model
model.save('saved_model/my_model')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup