{"id":342083,"date":"2022-12-02T15:00:17","date_gmt":"2022-12-02T15:00:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=342083"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=342083","title":{"rendered":"<span>Tensorflow: \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0440\u0430\u043d\u0441\u0444\u0435\u0440\u043d\u043e\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438 \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bfe\/a2f\/c11\/bfea2fc11a4e95d20a7f20598c502803.png\" width=\"780\" height=\"439\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bfe\/a2f\/c11\/bfea2fc11a4e95d20a7f20598c502803.png\"\/><figcaption><\/figcaption><\/figure>\n<div class=\"persona\" persona=\"true\"><img decoding=\"async\" persona=\"true\" class=\"image persona__image\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/c3c\/29d\/6a2\/c3c29d6a21b2db5f985f37973f0f7968.jpg\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c3c\/29d\/6a2\/c3c29d6a21b2db5f985f37973f0f7968.jpg\" data-blurred=\"true\"\/><\/p>\n<h5 class=\"persona__heading\" persona=\"true\">\u0410\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438: \u0420\u0443\u0441\u0442\u0435\u043c \u0413\u0430\u043b\u0438\u0435\u0432<\/h5>\n<p>IBM Senior DevOps Engineer &amp; Integration Architect<\/p>\n<\/div>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041d\u0430 \u0441\u0432\u044f\u0437\u0438 \u0420\u0443\u0441\u0442\u0435\u043c, IBM Senior DevOps Engineer &amp; Integration Architect.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0440\u0435\u043d\u0442\u0433\u0435\u043d\u043e\u0433\u0440\u0430\u0444\u0438\u0438 \u0433\u0440\u0443\u0434\u043d\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e <a href=\"https:\/\/www.cell.com\/cell\/fulltext\/S0092-8674%2818%2930154-5\">\u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f<\/a>, \u0441 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e MobileNet_v2 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 TensorFlow \u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u043e\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0440\u0435\u043d\u0442\u0433\u0435\u043d\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u043c\u0438 \u0433\u0440\u0443\u0434\u043d\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438.<\/p>\n<p>\u0426\u0435\u043b\u044c\u044e \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0432\u044b\u043a\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0433\u043a\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c.<\/p>\n<p>\u042f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u0430\u043c \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u044d\u0442\u043e!<\/p>\n<p><strong>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u0438 \u0434\u0430\u043d\u043d\u044b\u0445<\/strong><\/p>\n<p>\u042f \u0437\u043d\u0430\u044e, \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438, \u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438:<\/p>\n<pre><code class=\"python\">import numpy as np import os import pathlib import matplotlib.pylab as plt import tensorflow as tf import tensorflow_hub as hub import tensorflow_datasets as tfds tfds.disable_progress_bar() from tqdm import tqdm AUTOTUNE = tf.data.experimental.AUTOTUNE<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<pre><code class=\"python\">data_dir = pathlib.Path('tflite\/images') image_count_train = len(list(data_dir.glob('train\/*\/*.jpeg'))) image_count_test = len(list(data_dir.glob('test\/*\/*.jpeg'))) image_count_val = len(list(data_dir.glob('val\/*\/*.jpeg'))) BATCH_SIZE = 32 IMG_HEIGHT = 224 IMG_WIDTH = 224 IMG_SHAPE = (IMG_HEIGHT, IMG_WIDTH, 3) STEPS_PER_EPOCH = np.ceil(image_count_train\/BATCH_SIZE) EPOCHS = 10 SAVED_MODEL = \"pneumonia_saved_model\" (image_count_test, image_count_train, image_count_val)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f BATCH_SIZE \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043c\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f (\u043c\u0435\u0442\u043a\u0438):<\/p>\n<pre><code class=\"python\">CLASS_NAMES = np.array([item.name for item in data_dir.glob('train\/*') if item.name != \"LICENSE.txt\"]) num_classes = len(CLASS_NAMES) CLASS_NAMES<\/code><\/pre>\n<p>\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043f\u0430\u043f\u043a\u0430\u0445 train, test \u0438\u043b\u0438 val. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"python\">pneumonia = list(data_dir.glob('train\/PNEUMONIA\/*.jpeg'))  for image_path in pneumonia[:3]:     Image.open(str(image_path))<\/code><\/pre>\n<p><strong>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445<\/strong><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043d\u0430\u0431\u043e\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f. TensorFlow \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0435 \u0443\u0442\u0438\u043b\u0438\u0442\u044b \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 TensorFlow:<\/p>\n<pre><code class=\"python\">test_ds = tf.data.Dataset.list_files(str(data_dir\/'test\/*\/*')) train_ds = tf.data.Dataset.list_files(str(data_dir\/'train\/*\/*')) val_ds = tf.data.Dataset.list_files(str(data_dir\/'val\/*\/*')) for f in test_ds.take(5):   print(f.numpy())  print('Datasets loaded')<\/code><\/pre>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b: \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0449\u0435\u0439:<\/p>\n<p>\u00a0\u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043a\u0430\u043d\u0430\u043b\u044b RGB.<\/p>\n<p>\u00a0\u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043e \u043d\u0430\u0448\u0438\u0445 \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432.<\/p>\n<p>\u00a0\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443, \u0434\u0430\u044e\u0449\u0443\u044e 1 \u043e\u0434\u043d\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443 \u0438 0 \u0434\u0440\u0443\u0433\u043e\u043c\u0443.<\/p>\n<p>\u00a0\u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0448\u0430\u0433\u043e\u0432 \u043a \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430\u043c\u0438 \u0441 \u0438\u0445 \u043c\u0435\u0442\u043a\u0430\u043c\u0438.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044e\u0442\u0438\u043b\u0438\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<pre><code class=\"python\">def get_label(file_path):   parts = tf.strings.split(file_path, os.path.sep)   return parts[-2] == CLASS_NAMES[0]  def decode_img(img):   img = tf.image.decode_jpeg(img, channels=3)   img = tf.image.convert_image_dtype(img, tf.float32)   return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])  def process_path(file_path):   label = get_label(file_path)   img = tf.io.read_file(file_path)   img = decode_img(img)   return img, label  def format_image(image, label):     image = tf.image.resize(image, IMAGE_SIZE) \/ 255.0     return  image, label   print('Utility methods loaded!')<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 \u044d\u0442\u043e \u043a \u043d\u0430\u0448\u0438\u043c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0438\u043c\u0435\u043b\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0448\u0435\u0439\u043f\u044b:<\/p>\n<pre><code class=\"python\">train_examples = train_ds.map(process_path, num_parallel_calls=AUTOTUNE) test_examples = test_ds.map(process_path, num_parallel_calls=AUTOTUNE) validation_examples = val_ds.map(process_path, num_parallel_calls=AUTOTUNE) for image, label in test_examples.take(5):   print(\"Image shape: \", image.numpy().shape)   print(\"Label: \", label.numpy())   print('Check the shapes!')<\/code><\/pre>\n<p>\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0434\u0430\u0435\u0442 \u0442\u0435\u043d\u0437\u043e\u0440\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u0442\u0435\u043d\u0437\u043e\u0440\u043d\u0443\u044e \u043c\u0435\u0442\u043a\u0443.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043d\u0430 \u044d\u0442\u043e\u043c \u0448\u0430\u0433\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0430\u043a\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u044d\u0448 \u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u043f\u043e \u043c\u0435\u0440\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438! \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u043a\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432 10 \u0440\u0430\u0437 \u0438 \u0432\u0437\u044f\u0442 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437 \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u0430 Google:<\/p>\n<pre><code class=\"python\">def prepare_for_training(ds, cache=True, shuffle_buffer_size=1000):     if cache:         if isinstance(cache, str):             ds = ds.cache(cache)         else:             ds = ds.cache()     ds = ds.shuffle(buffer_size=shuffle_buffer_size)     ds = ds.repeat()     ds = ds.batch(BATCH_SIZE)     ds = ds.prefetch(buffer_size=AUTOTUNE)     return ds  train_examples_dataset = prepare_for_training(train_examples) test_examples_dataset = prepare_for_training(test_examples) validation_examples_dataset = prepare_for_training(validation_examples)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432<\/p>\n<p><code>image_batch, label_batch = next(iter(test_examples_dataset))<\/code><\/p>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0438!<\/p>\n<p><strong>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438<\/strong><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440, \u0432\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 \u043c\u043e\u0434\u0435\u043b\u0438!<\/p>\n<p>\u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u043b\u043e\u044f <code>feature_extractor_layer<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f Hub.<\/p>\n<p>\u0414\u043b\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441 \u043d\u0435\u043e\u0431\u0443\u0447\u0430\u0435\u043c\u043e\u0433\u043e feature_extractor_layer, \u043d\u043e \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0442\u043e\u043d\u043a\u0443\u044e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u041c\u043e\u0434\u0443\u043b\u0438-\u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f TensorFlow 1.x \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">module_selection = (\"mobilenet_v2\", 224, 1280) #or use [\"(\\\"mobilenet_v2\\\", 224, 1280)\", \"(\\\"inception_v3\\\", 299, 2048)\"] {type:\"raw\", allow-input: true}  handle_base, pixels, FV_SIZE = module_selection  MODULE_HANDLE =\"https:\/\/tfhub.dev\/google\/tf2-preview\/{}\/feature_vector\/4\".format(handle_base)  IMAGE_SIZE = (pixels, pixels)  print(\"Using {} with input size {} and output dimension {}\".format(MODULE_HANDLE, IMAGE_SIZE, FV_SIZE))<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432\u0435\u043a\u0442\u043e\u0440 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432, \u0430 \u043d\u0435 \u043f\u043e\u043b\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c. \u042d\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u043c \u0442\u043e\u043d\u043a\u043e\u0439 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 (\u0434\u043b\u044f \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c). \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0442\u043e\u0447\u043d\u0443\u044e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443, \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043f\u043e\u043b\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c (\u043e\u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 TensorFlow Hub).<\/p>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043c\u043e\u0434\u0443\u043b\u044c TFHub:<\/p>\n<pre><code class=\"python\">feature_extractor = hub.KerasLayer(MODULE_HANDLE,                                    input_shape=IMAGE_SIZE + (3,),                                    output_shape=[FV_SIZE],                                    trainable=False) feature_extractor.trainable = False print(\"Building model with\", MODULE_HANDLE) model = tf.keras.Sequential([ feature_extractor, tf.keras.layers.Dense(num_classes, activation='softmax')]) model.compile(optimizer='adam',                   loss='sparse_categorical_crossentropy',                   metrics=['accuracy']) model.summary()<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043d\u0430\u0448\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043d\u0435 \u043f\u043e\u0434\u0434\u0430\u044e\u0442\u0441\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e (\u0441\u043c. \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0438\u0437 MobileNet), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u043c. \u0414\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\">hist = model.fit(train_examples_dataset, epochs=EPOCHS, steps_per_epoch=image_count_train\/BATCH_SIZE, validation_steps=np.floor(image_count_val\/BATCH_SIZE), validation_data=validation_examples_dataset) tf.saved_model.save(model, SAVED_MODEL)<\/code><\/pre>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u043f\u0438\u0441\u044c, \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0432 \u0435\u0435 \u0441\u043d\u043e\u0432\u0430 \u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0432 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"python\">loaded = tf.saved_model.load(SAVED_MODEL) print(list(loaded.signatures.keys())) infer = loaded.signatures[\"serving_default\"] print(infer.structured_input_signature) print(infer.structured_outputs)<\/code><\/pre>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043c\u043e\u0436\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 TensorFlow \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 (\u0432\u043d\u0435 Python):<\/p>\n<p><code>saved_model_cli show --dir $1 --tag_set serve --signature_def serving_default<\/code><\/p>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b\u043e \u043d\u0435\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u043b\u0435\u0433\u043a\u043e; \u0441 \u0442\u0440\u0430\u043d\u0441\u0444\u0435\u0440\u043d\u044b\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e, \u0432 \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043a\u043e\u0434\u0430, \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0446\u0435\u043b\u044b\u0445 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0433\u0440\u0443\u043f\u043f!<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0447\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0440\u0430\u0431\u043e\u0442\u0435 \u043f\u043e \u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p><strong><em>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438 TensorFlow \u0432 TensorFlow Lite \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f<\/em><\/p>\n<p><\/strong>\u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0437 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e TensorFlow \u0432 TensorFlow Lite \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Python Converter API. \u042d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430\u0448\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0430 \u043f\u0435\u0440\u0438\u0444\u0435\u0440\u0438\u0439\u043d\u044b\u0445 \u0438 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445.<\/p>\n<p><strong>\u041a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u0430 TensorFlow Lite<\/strong><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 SavedModel, \u043f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043c\u043e\u0434\u0435\u043b\u044c TensorFlow Lite, \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"python\">import numpy as np import os import pathlib import matplotlib.pylab as plt import tensorflow as tf import tensorflow_hub as hub import tensorflow_datasets as tfds tfds.disable_progress_bar() from tqdm import tqdm AUTOTUNE = tf.data.experimental.AUTOTUNE  SAVED_MODEL = \"pneumonia_saved_model\" converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL)<\/code><\/pre>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440 \u0438\u0437 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 SavedModel, ConcreteFunction \u0438\u043b\u0438 Keras!<\/p>\n<p><strong>\u041a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f<\/strong><\/p>\n<p>\u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0430\u044f \u0444\u043e\u0440\u043c\u0430 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u0432\u0430\u043d\u0442\u0443\u0435\u0442 \u043e\u0442 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0434\u043e 8-\u0431\u0438\u0442\u043d\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438. \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u043a\u043b\u044e\u0447\u0435\u043d \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u0435 TensorFlow Lite. \u041f\u0440\u0438 \u0432\u044b\u0432\u043e\u0434\u0435 \u0432\u0435\u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0438\u0437 8-\u0431\u0438\u0442\u043d\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044e\u0442\u0441\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u044f\u0434\u0435\u0440 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439. \u042d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438.<\/p>\n<pre><code class=\"python\">converter.optimizations = [tf.lite.Optimize.DEFAULT]<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u043d\u0430 \u043f\u0443\u0442\u0435\u043c \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438. \u0415\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\">converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]<\/code><\/pre>\n<p>\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c, \u0438 \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0430:<\/p>\n<pre><code class=\"python\">tflite_model = converter.convert() tflite_model_file = 'converted_model.tflite'  with open(tflite_model_file, \"wb\") as f:     f.write(tflite_model)  print('Done quantizing')<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b\u043e \u0443\u0434\u0438\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u0431\u044b\u0441\u0442\u0440\u043e, \u0445\u043e\u0442\u044f \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0437\u0432\u0443\u0447\u0438\u0442 \u0441\u043b\u043e\u0436\u043d\u043e \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u043d\u0430 \u0431\u044d\u043a\u044d\u043d\u0434\u0435, \u043d\u043e \u0435\u0435 \u0432\u0435\u0441\u044c\u043c\u0430 \u043b\u0435\u0433\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430<\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435:<\/p>\n<pre><code class=\"python\">from pathlib import Path  saved_model = Path(SAVED_MODEL) full_model_size = sum(f.stat().st_size for f in saved_model.glob('**\/*') if f.is_file() )\/(1024*1024) print(f'Full model size {full_model_size} MB') converted_model = Path(tflite_model_file) converted_model_size = converted_model.stat().st_size \/ (1024*1024) print(f'Converted model size {converted_model_size} MB')<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/5b2\/511\/9a5\/5b25119a5c36395ea8561a233b19d360.png\" width=\"940\" height=\"160\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5b2\/511\/9a5\/5b25119a5c36395ea8561a233b19d360.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0437\u0430 \u043e\u0434\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u043e\u0447\u0442\u0438 \u043d\u0430 80%<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438, \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u0438\u043a\u043e\u0432\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u044b\u043c \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u0435\u043b\u044f\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u0443\u0431\u0435\u0434\u0438\u0432\u0448\u0438\u0441\u044c, \u0447\u0442\u043e \u0432\u0441\u044f \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u0440\u0438\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0439 \u0438 \u0432\u0445\u043e\u0434\u043e\u0432 \u0441 \u0440\u0435\u043f\u0440\u0435\u0437\u0435\u043d\u0442\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u0442\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u0435 \u0435\u0433\u043e \u0432\u0430\u0448\u0435\u043c\u0443 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u0443:<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u0435\u0440\u043d\u0451\u043c \u043d\u0430\u0448 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n<pre><code class=\"python\">def get_label(file_path):   parts = tf.strings.split(file_path, os.path.sep)   return parts[-2] == CLASS_NAMES[0]  @tf.autograph.experimental.do_not_convert def decode_img(img):   img = tf.image.decode_jpeg(img, channels=3)   img = tf.image.convert_image_dtype(img, tf.float32)   return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])  @tf.autograph.experimental.do_not_convert def process_path(file_path):   label = get_label(file_path)   img = tf.io.read_file(file_path)   img = decode_img(img)   return img, label  def format_image(image, label):     image = tf.image.resize(image, IMAGE_SIZE) \/ 255.0     return  image, label  def prepare_for_training(ds, cache=True, shuffle_buffer_size=1000):     if cache:         if isinstance(cache, str):             ds = ds.cache(cache)         else:             ds = ds.cache()     ds = ds.shuffle(buffer_size=shuffle_buffer_size)     ds = ds.repeat()     ds = ds.batch(BATCH_SIZE)     ds = ds.prefetch(buffer_size=AUTOTUNE)     return ds  data_dir = pathlib.Path('tflite\/images') BATCH_SIZE = 32 IMG_HEIGHT = 224 IMG_WIDTH = 224 IMG_SHAPE = (IMG_HEIGHT, IMG_WIDTH, 3) CLASS_NAMES = np.array([item.name for item in data_dir.glob('train\/*') if item.name != \"LICENSE.txt\"]) test_ds = tf.data.Dataset.list_files(str(data_dir\/'test\/*\/*')) test_examples = test_ds.map(process_path, num_parallel_calls=AUTOTUNE) test_examples_dataset = prepare_for_training(test_examples)<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0440\u0435\u043f\u0440\u0435\u0437\u0435\u043d\u0442\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b \u043f\u043e \u043c\u0435\u0440\u0435 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0435\u0441\u0430 \u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0438 \u0432 INT8:<\/p>\n<pre><code class=\"python\">def representative_data_gen():     for image_batch, label_batch in test_examples_dataset.take(1):         for image in image_batch:             yield [[image]]  len(list(representative_data_gen())) converter.representative_dataset = representative_data_gen<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0430, \u043d\u043e \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439.<\/p>\n<p>\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0441\u0442\u0430\u043d\u0443\u0442\u0441\u044f \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0434\u043a\u043e, \u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u0435 \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u0435\u043b\u044f\u043c\u0438, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0449\u0438\u043c\u0438 float.<\/p>\n<p><em>\u041f\u043e\u043b\u043d\u043e\u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0435 (\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043b\u044f \u0437\u043d\u0430\u043d\u0438\u044f)<\/em><\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u0434\u0430\u0432\u0430\u043b \u0442\u043e\u043b\u044c\u043a\u043e \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c:<\/p>\n<p><code>converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]<\/code><\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0438\u043c\u0435\u0439\u0442\u0435 \u0432 \u0432\u0438\u0434\u0443, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u0443\u044e INT8-\u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0441 \u0432\u0430\u0448\u0435\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e, \u043e\u043d \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u042d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d, \u043d\u043e \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438 \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u0438 \u043d\u0430 TPU, \u043e\u043d \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 INT8.<\/p>\n<p>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438<\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c:<\/p>\n<pre><code class=\"python\">tflite_model = converter.convert() tflite_model_file = 'converted_model_int8.tflite'  with open(tflite_model_file, \"wb\") as f:     f.write(tflite_model)  print('Done quantizing with Representative Dataset')<\/code><\/pre>\n<p><strong>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432<\/strong><\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043d\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u0430\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043c\u0435\u043d\u044c\u0448\u0435, \u043d\u043e \u044d\u0442\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0442\u0430\u043a. \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 INT8 \u0432 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0435\u043d\u043e \u043d\u0430 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u0445 \u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"python\">from pathlib import Path  quantized_weights = Path('converted_model.tflite') weights_quantized_size = quantized_weights.stat().st_size\/(1024*1024) print(f'Quantized for weights model size {weights_quantized_size} MB')  weights_and_activations_model = Path('converted_model_int8.tflite') weights_and_activations_model_size = weights_and_activations_model.stat().st_size\/(1024*1024) print(f'Quantized for weights and activations size {weights_and_activations_model_size} MB')<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/714\/431\/c5a\/714431c5a04434c45615d9a3f73b1735.png\" width=\"1183\" height=\"311\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/714\/431\/c5a\/714431c5a04434c45615d9a3f73b1735.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0418 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043e\u0431\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b. \u041d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e!<\/p>\n<p><strong>\u041f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c TensorFlow Lite \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u0430 Python.<\/strong><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0430\u0448\u0438 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0438\u0445 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c!<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0435\u0441\u043e\u0432. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0435\u043d\u0437\u043e\u0440\u044b \u0434\u043b\u044f \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u043e\u0432:<\/p>\n<pre><code class=\"python\">weights_tflite_model_file = 'converted_model.tflite'  interpreter = tf.lite.Interpreter(model_path=weights_tflite_model_file) interpreter.allocate_tensors()  input_index = interpreter.get_input_details()[0][\"index\"] output_index = interpreter.get_output_details()[0][\"index\"]<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043f\u0430\u0440\u0442\u0438\u044e \u0438\u0437 15 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 (\u0438\u0437 \u0441\u043e\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438) \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0435\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438:<\/p>\n<pre><code class=\"python\">import time start_time = time.time() predictions = []  test_labels, test_imgs = [], [] debug = 0 image_batch, label_batch = next(iter(test_examples_dataset)) for img, label in zip(image_batch, label_batch):     debug += 1     if debug % 5 == 1:         print(f'I am treating image {debug} with label {label}')     if debug == 15:         break     interpreter.set_tensor(input_index, np.array([img]))     interpreter.invoke()     predictions.append(interpreter.get_tensor(output_index))     test_labels.append(label.numpy())     test_imgs.append(img)   print(f'Predictions calculated in {time.time() - start_time} seconds')<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/252\/a6a\/ec4\/252a6aec4d00041735380e9dddd5ba46.png\" width=\"1019\" height=\"574\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/252\/a6a\/ec4\/252a6aec4d00041735380e9dddd5ba46.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u044b. \u0420\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"python\">ok_value = 0 wrong_value = 0 true_positives = 0 total = 0 true_negatives = 0 false_positives = 0 false_negatives = 0 for predictions_array, true_label in zip(predictions, test_labels):     predicted_label = np.argmax(predictions_array)     if predicted_label == true_label:         ok_value += 1         if CLASS_NAMES[int(true_label)] == 'NORMAL':             true_negatives += 1         else:             true_positives += 1     else:         wrong_value += 1         if CLASS_NAMES[predicted_label] == 'NORMAL':             false_negatives +=1         else:             false_positives += 1     total += 1   print(f'Accuracy: {(true_positives + true_negatives) \/ total} \\n ') print(f'Sensitivity: {true_positives\/ (true_positives + false_negatives)} \\n ') print(f'Specificity: {true_negatives \/ (true_negatives + false_positives)}')<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/006\/349\/dcc\/006349dccbfa1b7fc2de5e4b21a5741f.png\" width=\"1110\" height=\"224\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/006\/349\/dcc\/006349dccbfa1b7fc2de5e4b21a5741f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041d\u0430\u0448\u0430 \u043c\u043e\u0434\u0435\u043b\u044c \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u0430: \u0435\u0435, \u043d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c, \u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0437\u0430 30 \u043c\u0438\u043d\u0443\u0442 \u2014 \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e!<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0435\u0441\u043e\u0432 \u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0439:<\/p>\n<pre><code class=\"python\">weights_tflite_model_file = 'converted_model_int8.tflite'  interpreter = tf.lite.Interpreter(model_path=weights_tflite_model_file) interpreter.allocate_tensors()  input_index = interpreter.get_input_details()[0][\"index\"] output_index = interpreter.get_output_details()[0][\"index\"]<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u043f\u0430\u0440\u0442\u0438\u044e \u0438\u0437 15 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 (\u043e\u043f\u044f\u0442\u044c \u0436\u0435, \u0438\u0437 \u0441\u043e\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438) \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u0435\u0435 \u043c\u0435\u0442\u0440\u0438\u043a\u0438:<\/p>\n<pre><code class=\"python\">import  time start_time = time.time() predictions = []  test_labels, test_imgs = [], [] debug = 0 image_batch, label_batch = next(iter(test_examples_dataset)) for img, label in zip(image_batch, label_batch):     debug += 1     if debug % 5 == 1:         print(f'I am treating image {debug} with label {label}')     if debug == 15:         break     interpreter.set_tensor(input_index, np.array([img]))     interpreter.invoke()     predictions.append(interpreter.get_tensor(output_index))     test_labels.append(label.numpy())     test_imgs.append(img)   print(f'Predictions calculated in {time.time()  -  start_time} seconds')<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/1e2\/311\/1a3\/1e23111a3ee54c7158cdcb9596801924.png\" width=\"1078\" height=\"170\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1e2\/311\/1a3\/1e23111a3ee54c7158cdcb9596801924.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u044b. \u0420\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c, \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0438 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u043e\u0441\u0442\u044c:<\/p>\n<pre><code class=\"python\">ok_value = 0 wrong_value = 0 true_positives = 0 total = 0 true_negatives = 0 false_positives = 0 false_negatives = 0 for predictions_array, true_label in zip(predictions, test_labels):     predicted_label = np.argmax(predictions_array)     if predicted_label == true_label:         ok_value += 1         if CLASS_NAMES[int(true_label)] == 'NORMAL':             true_negatives += 1         else:             true_positives += 1     else:         wrong_value += 1         if CLASS_NAMES[predicted_label] == 'NORMAL':             false_negatives +=1         else:             false_positives += 1     total += 1   print(f'Accuracy: {(true_positives + true_negatives) \/ total} \\n ') print(f'Sensitivity: {true_positives\/ (true_positives + false_negatives)} \\n ') print(f'Specificity: {true_negatives \/ (true_negatives + false_positives)}')<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/81b\/805\/f95\/81b805f9575b5b43e2eaa92796e9ab27.png\" width=\"1151\" height=\"222\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/81b\/805\/f95\/81b805f9575b5b43e2eaa92796e9ab27.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e, \u0445\u043e\u0442\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0432\u0435\u0441\u043e\u0432 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \u0438 \u043b\u0443\u0447\u0448\u0435, \u043c\u043e\u0434\u0435\u043b\u044c INT8 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u0418\u0434\u0435\u044f \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f INT8 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u0435\u0440\u044f\u0442\u044c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c (\u043d\u0435\u043c\u043d\u043e\u0433\u043e) \u0434\u043b\u044f \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438.<\/p>\n<p>\u0420\u0435\u0437\u044e\u043c\u0438\u0440\u0443\u044f:<\/p>\n<p>\u041c\u044b \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043b\u0438 \u043d\u0430\u0431\u043e\u0440 \u0440\u0435\u043d\u0442\u0433\u0435\u043d\u043e\u0432\u0441\u043a\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u0438\u0438.<\/p>\n<p>\u00a0\u041c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430\u0431\u043e\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 TensorFlow, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0438\u0437 \u043d\u0430\u0448\u0438\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p>\u00a0\u041c\u044b \u0443\u0437\u043d\u0430\u043b\u0438, \u043a\u0430\u043a \u0430\u0434\u0430\u043f\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u043d\u0430\u0431\u043e\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u043b\u0438 \u043f\u0435\u0440\u0435\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043e 224 x 224 x 3 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043c\u0435\u0442\u043a\u0443, \u043f\u0440\u0438\u0447\u0435\u043c \u0432\u0441\u0435 \u0432 \u043f\u0430\u043a\u0435\u0442\u043d\u043e\u043c \u0440\u0435\u0436\u0438\u043c\u0435.<\/p>\n<p>\u00a0\u041c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438, \u043e\u0431\u0443\u0447\u0438\u043b\u0438 \u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0438 \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435\u0439 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f MobileNet v2 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043b\u043e\u0439 softmax \u043d\u0430\u0434 \u043d\u0438\u043c.<\/p>\n<p>\u041c\u044b \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043b\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438 \u0434\u043b\u044f \u0432\u0435\u0441\u043e\u0432 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>\u00a0\u041c\u044b \u043f\u043e\u0434\u0442\u0432\u0435\u0440\u0434\u0438\u043b\u0438, \u0447\u0442\u043e \u043c\u044b \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0430 80%, \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0435\u043b\u0430\u044f \u044d\u0442\u043e.<\/p>\n<p>\u00a0\u041c\u044b \u0443\u0437\u043d\u0430\u043b\u0438 \u043e \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0438 \u0430\u043a\u0442\u0438\u0432\u0430\u0446\u0438\u0439 \u0441 \u0440\u0435\u043f\u0440\u0435\u0437\u0435\u043d\u0442\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0438 \u0441 \u043f\u043e\u043b\u043d\u044b\u043c \u0446\u0435\u043b\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c.<\/p>\n<p>\u00a0\u041c\u044b \u043e\u0446\u0435\u043d\u0438\u043b\u0438 \u043e\u0431\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0430 \u0432 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u0441\u0442\u0430\u0440\u0442\u0430 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/DSuI\/\">Machine Learning. Professional<\/a>, \u0445\u043e\u0447\u0443 \u043f\u0440\u0438\u0433\u043b\u0430\u0441\u0438\u0442\u044c \u0432\u0441\u0435\u0445 \u043d\u0430 <a href=\"https:\/\/otus.pw\/DSuI\/\">\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0443\u0440\u043e\u043a \u043a\u0443\u0440\u0441\u0430<\/a>, \u0433\u0434\u0435 \u043f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u0438 OTUS \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443\u0442 \u043a\u0430\u043a\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b \u043a \u0430\u043d\u0441\u0430\u043c\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0438, \u043a\u0430\u043a \u0443\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u0442\u0430\u043a\u0438\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0435 \u0442\u0435\u0445\u043d\u0438\u043a\u0438 \u0430\u043d\u0441\u0430\u043c\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u043a Bagging, Random Forest \u0438 Gradient Boosting. \u041a\u043e\u0433\u0434\u0430 \u0438 \u043a\u0430\u043a \u0438\u0445 \u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f ML-\u0437\u0430\u0434\u0430\u0447.<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/otus.pw\/DSuI\/\">\u0417\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0439 \u0443\u0440\u043e\u043a<\/a><\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/703186\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/703186\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<div class=\"persona\" persona=\"true\">\n<h5 class=\"persona__heading\" persona=\"true\">\u0410\u0432\u0442\u043e\u0440 \u0441\u0442\u0430\u0442\u044c\u0438: \u0420\u0443\u0441\u0442\u0435\u043c \u0413\u0430\u043b\u0438\u0435\u0432<\/h5>\n<p>IBM Senior DevOps Engineer &amp; Integration Architect<\/p>\n<\/div>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041d\u0430 \u0441\u0432\u044f\u0437\u0438 \u0420\u0443\u0441\u0442\u0435\u043c, IBM Senior DevOps Engineer &amp; Integration Architect.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u043d\u0430\u0431\u043e\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0440\u0435\u043d\u0442\u0433\u0435\u043d\u043e\u0433\u0440\u0430\u0444\u0438\u0438 \u0433\u0440\u0443\u0434\u043d\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e <a href=\"https:\/\/www.cell.com\/cell\/fulltext\/S0092-8674%2818%2930154-5\">\u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f<\/a>, \u0441 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e MobileNet_v2 \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 TensorFlow \u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u043e\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0440\u0435\u043d\u0442\u0433\u0435\u043d\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u043c\u0438 \u0433\u0440\u0443\u0434\u043d\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438.<\/p>\n<p>\u0426\u0435\u043b\u044c\u044e \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0432\u044b\u043a\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0433\u043a\u043e \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c.<\/p>\n<p>\u042f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u0430\u043c \u043f\u043e\u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f \u044d\u0442\u043e!<\/p>\n<p><strong>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0443\u043b\u0435\u0439 \u0438 \u0434\u0430\u043d\u043d\u044b\u0445<\/strong><\/p>\n<p>\u042f \u0437\u043d\u0430\u044e, \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438, \u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043c\u043e\u0434\u0443\u043b\u0438:<\/p>\n<pre><code class=\"python\">import numpy as np import os import pathlib import matplotlib.pylab as plt import tensorflow as tf import tensorflow_hub as hub import tensorflow_datasets as tfds tfds.disable_progress_bar() from tqdm import tqdm AUTOTUNE = tf.data.experimental.AUTOTUNE<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u044c.<\/p>\n<pre><code class=\"python\">data_dir = pathlib.Path('tflite\/images') image_count_train = len(list(data_dir.glob('train\/*\/*.jpeg'))) image_count_test = len(list(data_dir.glob('test\/*\/*.jpeg'))) image_count_val = len(list(data_dir.glob('val\/*\/*.jpeg'))) BATCH_SIZE = 32 IMG_HEIGHT = 224 IMG_WIDTH = 224 IMG_SHAPE = (IMG_HEIGHT, IMG_WIDTH, 3) STEPS_PER_EPOCH = np.ceil(image_count_train\/BATCH_SIZE) EPOCHS = 10 SAVED_MODEL = \"pneumonia_saved_model\" (image_count_test, image_count_train, image_count_val)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f BATCH_SIZE \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u043c\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f (\u043c\u0435\u0442\u043a\u0438):<\/p>\n<pre><code class=\"python\">CLASS_NAMES = np.array([item.name for item in data_dir.glob('train\/*') if item.name != \"LICENSE.txt\"]) num_classes = len(CLASS_NAMES) CLASS_NAMES<\/code><\/pre>\n<p>\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043f\u0430\u043f\u043a\u0430\u0445 train, test \u0438\u043b\u0438 val. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c:<\/p>\n<pre><code class=\"python\">pneumonia = list(data_dir.glob('train\/PNEUMONIA\/*.jpeg'))  for image_path in pneumonia[:3]:     Image.open(str(image_path))<\/code><\/pre>\n<p><strong>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445<\/strong><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043e, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u043d\u0430\u0431\u043e\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f. TensorFlow \u043f\u0440\u0435\u0434\u043b\u0430\u0433\u0430\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0435 \u0443\u0442\u0438\u043b\u0438\u0442\u044b \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 TensorFlow:<\/p>\n<pre><code class=\"python\">test_ds = tf.data.Dataset.list_files(str(data_dir\/'test\/*\/*')) train_ds = tf.data.Dataset.list_files(str(data_dir\/'train\/*\/*')) val_ds = tf.data.Dataset.list_files(str(data_dir\/'val\/*\/*')) for f in test_ds.take(5):   print(f.numpy())  print('Datasets loaded')<\/code><\/pre>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b: \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0449\u0435\u0439:<\/p>\n<p>\u00a0\u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043a\u0430\u043d\u0430\u043b\u044b RGB.<\/p>\n<p>\u00a0\u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043e \u043d\u0430\u0448\u0438\u0445 \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u0432.<\/p>\n<p>\u00a0\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443, \u0434\u0430\u044e\u0449\u0443\u044e 1 \u043e\u0434\u043d\u043e\u043c\u0443 \u043a\u043b\u0430\u0441\u0441\u0443 \u0438 0 \u0434\u0440\u0443\u0433\u043e\u043c\u0443.<\/p>\n<p>\u00a0\u041c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0448\u0430\u0433\u043e\u0432 \u043a \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430\u043c\u0438 \u0441 \u0438\u0445 \u043c\u0435\u0442\u043a\u0430\u043c\u0438.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044e\u0442\u0438\u043b\u0438\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b:<\/p>\n<pre><code class=\"python\">def get_label(file_path):   parts = tf.strings.split(file_path, os.path.sep)   return parts[-2] == CLASS_NAMES[0]  def decode_img(img):   img = tf.image.decode_jpeg(img, channels=3)   img = tf.image.convert_image_dtype(img, tf.float32)   return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])  def process_path(file_path):   label = get_label(file_path)   img = tf.io.read_file(file_path)   img = decode_img(img)   return img, label  def format_image(image, label):     image = tf.image.resize(image, IMAGE_SIZE) \/ 255.0     return  image, label   print('Utility methods loaded!')<\/code><\/pre>\n<p>\u041f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 \u044d\u0442\u043e \u043a \u043d\u0430\u0448\u0438\u043c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0442\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0438\u043c\u0435\u043b\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0448\u0435\u0439\u043f\u044b:<\/p>\n<pre><code class=\"python\">train_examples = train_ds.map(process_path, num_parallel_calls=AUTOTUNE) test_examples = test_ds.map(process_path, num_parallel_calls=AUTOTUNE) validation_examples = val_ds.map(process_path, num_parallel_calls=AUTOTUNE) for image, label in test_examples.take(5):   print(\"Image shape: \", image.numpy().shape)   print(\"Label: \", label.numpy())   print('Check the shapes!')<\/code><\/pre>\n<p>\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u044b\u0434\u0430\u0435\u0442 \u0442\u0435\u043d\u0437\u043e\u0440\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u0442\u0435\u043d\u0437\u043e\u0440\u043d\u0443\u044e \u043c\u0435\u0442\u043a\u0443.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043d\u0430 \u044d\u0442\u043e\u043c \u0448\u0430\u0433\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0430\u043a\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u044d\u0448 \u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u043f\u043e \u043c\u0435\u0440\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438! \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u043a\u0435\u0442\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432 10 \u0440\u0430\u0437 \u0438 \u0432\u0437\u044f\u0442 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438\u0437 \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u0430 Google:<\/p>\n<pre><code class=\"python\">def prepare_for_training(ds, cache=True, shuffle_buffer_size=1000):     if cache:         if isinstance(cache, str):             ds = ds.cache(cache)         else:             ds = ds.cache()     ds = ds.shuffle(buffer_size=shuffle_buffer_size)     ds = ds.repeat()     ds = ds.batch(BATCH_SIZE)     ds = ds.prefetch(buffer_size=AUTOTUNE)     return ds  train_examples_dataset = prepare_for_training(train_examples) test_examples_dataset = prepare_for_training(test_examples) validation_examples_dataset = prepare_for_training(validation_examples)<\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432<\/p>\n<p><code>image_batch, label_batch = next(iter(test_examples_dataset))<\/code><\/p>\n<p>\u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0438!<\/p>\n<p><strong>\u041e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438<\/strong><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440, \u0432\u0441\u0435 \u0434\u0435\u043b\u043e \u0432 \u043c\u043e\u0434\u0435\u043b\u0438!<\/p>\n<p>\u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043f\u043e\u0432\u0435\u0440\u0445 \u0441\u043b\u043e\u044f <code>feature_extractor_layer<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0434\u0443\u043b\u044f Hub.<\/p>\n<p>\u0414\u043b\u044f \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441 \u043d\u0435\u043e\u0431\u0443\u0447\u0430\u0435\u043c\u043e\u0433\u043e feature_extractor_layer, \u043d\u043e \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0442\u043e\u043d\u043a\u0443\u044e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>\u041c\u043e\u0434\u0443\u043b\u0438-\u043a\u043e\u043d\u0446\u0435\u043d\u0442\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f TensorFlow 1.x \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">module_selection = (\"mobilenet_v2\", 224, 1280) #or use [\"(\\\"mobilenet_v2\\\", 224, 1280)\", \"(\\\"inception_v3\\\", 299, 2048)\"] {type:\"raw\", allow-input: true}  handle_base, pixels, FV_SIZE = module_selection  MODULE_HANDLE =\"https:\/\/tfhub.dev\/google\/tf2-preview\/{}\/feature_vector\/4\".format(handle_base)  IMAGE_SIZE = (pixels, pixels)  print(\"Using {} with input size {} and output dimension {}\".format(MODULE_HANDLE, IMAGE_SIZE, FV_SIZE))<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0432\u0435\u043a\u0442\u043e\u0440 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u043e\u0432, \u0430 \u043d\u0435 \u043f\u043e\u043b\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c. \u042d\u0442\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u043c \u0442\u043e\u043d\u043a\u043e\u0439 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 (\u0434\u043b\u044f \u0438\u0437\u0431\u0435\u0436\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c). \u041e\u0434\u043d\u0430\u043a\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0442\u043e\u0447\u043d\u0443\u044e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443, \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043f\u043e\u043b\u043d\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c (\u043e\u043d\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 TensorFlow Hub).<\/p>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u043c\u043e\u0434\u0443\u043b\u044c TFHub:<\/p>\n<pre><code class=\"python\">feature_extractor = hub.KerasLayer(MODULE_HANDLE,                                    input_shape=IMAGE_SIZE + (3,),                                    output_shape=[FV_SIZE],                                    trainable=False) feature_extractor.trainable = False print(\"Building model with\", MODULE_HANDLE) model = tf.keras.Sequential([ feature_extractor, tf.keras.layers.Dense(num_classes, activation='softmax')]) model.compile(optimizer='adam',                   loss='sparse_categorical_crossentropy',                   metrics=['accuracy']) model.summary()<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0438\u0434\u0438\u0442\u0435, \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u043d\u0430\u0448\u0438\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043d\u0435 \u043f\u043e\u0434\u0434\u0430\u044e\u0442\u0441\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e (\u0441\u043c. \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0438\u0437 MobileNet), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u044b\u043c. \u0414\u043b\u044f \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\">hist = model.fit(train_examples_dataset, epochs=EPOCHS, steps_per_epoch=image_count_train\/BATCH_SIZE, validation_steps=np.floor(image_count_val\/BATCH_SIZE), validation_data=validation_examples_dataset) tf.saved_model.save(model, SAVED_MODEL)<\/code><\/pre>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u043c\u0435\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u043f\u0438\u0441\u044c, \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0432 \u0435\u0435 \u0441\u043d\u043e\u0432\u0430 \u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u0432 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"python\">loaded = tf.saved_model.load(SAVED_MODEL) print(list(loaded.signatures.keys())) infer = loaded.signatures[\"serving_default\"] print(infer.structured_input_signature) print(infer.structured_outputs)<\/code><\/pre>\n<p>\u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043c\u043e\u0436\u0435\u0442\u0435 \u043b\u0438 \u0432\u044b \u0442\u0430\u043a\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 TensorFlow \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 (\u0432\u043d\u0435 Python):<\/p>\n<p><code>saved_model_cli show --dir $1 --tag_set serve --signature_def serving_default<\/code><\/p>\n<p>\u042d\u0442\u043e \u0431\u044b\u043b\u043e \u043d\u0435\u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u043b\u0435\u0433\u043a\u043e; \u0441 \u0442\u0440\u0430\u043d\u0441\u0444\u0435\u0440\u043d\u044b\u043c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e, \u0432 \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043a\u043e\u0434\u0430, \u043f\u0440\u043e\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0446\u0435\u043b\u044b\u0445 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u0433\u0440\u0443\u043f\u043f!<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0447\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0440\u0430\u0431\u043e\u0442\u0435 \u043f\u043e \u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p><strong><em>\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043f\u043d\u0435\u0432\u043c\u043e\u043d\u0438\u0438 TensorFlow \u0432 TensorFlow Lite \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f<\/em><\/p>\n<p><\/strong>\u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043b\u0435\u0433\u043a\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0438\u0437 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e TensorFlow \u0432 TensorFlow Lite \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Python Converter API. \u042d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043d\u0430\u0448\u0438\u0445 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0430 \u043f\u0435\u0440\u0438\u0444\u0435\u0440\u0438\u0439\u043d\u044b\u0445 \u0438 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430\u0445.<\/p>\n<p><strong>\u041a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u0430 TensorFlow Lite<\/strong><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 SavedModel, \u043f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043c\u043e\u0434\u0435\u043b\u044c TensorFlow Lite, \u2014 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f:<\/p>\n<pre><code class=\"python\">import numpy as np import os import pathlib import matplotlib.pylab as plt import tensorflow as tf import tensorflow_hub as hub import tensorflow_datasets as tfds tfds.disable_progress_bar() from tqdm import tqdm AUTOTUNE = tf.data.experimental.AUTOTUNE  SAVED_MODEL = \"pneumonia_saved_model\" converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL)<\/code><\/pre>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440 \u0438\u0437 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 SavedModel, ConcreteFunction \u0438\u043b\u0438 Keras!<\/p>\n<p><strong>\u041a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f<\/strong><\/p>\n<p>\u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0430\u044f \u0444\u043e\u0440\u043c\u0430 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u0432\u0430\u043d\u0442\u0443\u0435\u0442 \u043e\u0442 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0434\u043e 8-\u0431\u0438\u0442\u043d\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438. \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u043a\u043b\u044e\u0447\u0435\u043d \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u0446\u0438\u0438 \u0432 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0435\u0440\u0435 TensorFlow Lite. \u041f\u0440\u0438 \u0432\u044b\u0432\u043e\u0434\u0435 \u0432\u0435\u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442\u0441\u044f \u0438\u0437 8-\u0431\u0438\u0442\u043d\u043e\u0439 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0432 \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u044e\u0442\u0441\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u044f\u0434\u0435\u0440 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439. \u042d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438.<\/p>\n<pre><code class=\"python\">converter.optimizations = [tf.lite.Optimize.DEFAULT]<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u044b\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u043d\u0430 \u043f\u0443\u0442\u0435\u043c \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0438. \u0415\u0441\u043b\u0438 \u0431\u044b \u043c\u044b \u0445\u043e\u0442\u0435\u043b\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043c\u0435\u0440, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\">converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]<\/code><\/pre>\n<p>\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0448\u0443 \u043c\u043e\u0434\u0435\u043b\u044c, \u0438 \u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u043a\u0432\u0430\u043d\u0442\u043e\u0432\u0430\u043d\u0430:<\/p>\n<pre><code class=\"python\">tflite_model = converter.convert() tflite_model_file = 'converted_model.tflite'  with open(tflite_model_file, \"wb\") as f:<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-342083","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/342083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342083"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/342083\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}