Итак, я использую GCP с процессором и графическим процессором, но решил попробовать TPU, поэтому я начал создавать виртуальную машину, затем установить все необходимые библиотеки python, затем создать TPU и попробовать с этой треской (Image Generi c Classification), (Я уже пробовал этот код в Google Collab и работал, но на реальном TPU получил ошибку tan
%tensorflow_version 2.x
import tensorflow as tf
tf.compat.v1.enable_eager_execution()
# importing libraries
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
print("TF version:", tf.__version__)
import os
import pprint # for pretty printing our device stats
if 'COLAB_TPU_ADDR' not in os.environ:
print('ERROR: Not connected to a TPU runtime; please see the first cell in this notebook for instructions!')
else:
tpu_address = 'grpc://' + os.environ['tpu1']
print ('TPU address is', tpu_address)
with tf.compat.v1.Session(tpu_address) as session:
devices = session.list_devices()
print('TPU devices:')
pprint.pprint(devices)
tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection
print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])
tf.config.experimental_connect_to_cluster(tpu)
tf.tpu.experimental.initialize_tpu_system(tpu)
strategy = tf.distribute.experimental.TPUStrategy(tpu)
img_width, img_height = 224, 224
train_data_dir = 'dataset/training_set/'
validation_data_dir = 'dataset/test_set'
nb_train_samples = 801
nb_validation_samples = 201
epochs = 10
batch_size = 16
if K.image_data_format() == 'channels_first':
input_shape = (3, img_width, img_height)
else:
input_shape = (img_width, img_height, 3)
with strategy.scope():
model = Sequential()
model.add(Conv2D(32, (2, 2), input_shape = input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Conv2D(32, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Conv2D(64, (2, 2)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size =(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss ='binary_crossentropy',
optimizer ='rmsprop',
metrics =['accuracy'])
train_datagen = ImageDataGenerator(
rescale = 1. / 255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1. / 255)
train_generator = train_datagen.flow_from_directory(train_data_dir,
target_size =(img_width, img_height),
batch_size = batch_size, class_mode ='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size =(img_width, img_height),
batch_size = batch_size, class_mode ='binary')
model.fit_generator(train_generator,
steps_per_epoch = nb_train_samples // batch_size,
epochs = epochs, validation_data = validation_generator,
validation_steps = nb_validation_samples // batch_size)
model.evaluate(validation_generator,batch_size=None)
model.save_weights('model_saved.h5')
Как видите, я установил имя TPU «tpu1». Я не знаю, что не так, уже попробуйте это
gcloud config set project your-project-name
gcloud config set compute/zone us-central1-b
and yes the TPU is ready already tried with --> gcloud compute tpus list
Traceback (most recent call last):
File "cnn2.py", line 14, in <module>
tpu = tf.contrib.cluster_resolver.TPUClusterResolver() # TPU detection
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/distribute/cluster_resolver/tpu_cluster_resolver.py", line 264, in __init__
raise RuntimeError('You need to specify a TPU Name if you are running in '
RuntimeError: You need to specify a TPU Name if you are running in the Google Cloud environment.