#python #tensorflow
Вопрос:
Я новичок в Tensorflow и хотел бы перенести код Tensorflow 1 в Tensorflow 2 в Python 3.9.7.
К сожалению, я не могу заставить это работать даже после некоторых исследований и поиска stackoverflow
Я думаю, что проблема в том, чтобы соответствовать tf.train.string_input_producer
. Это также сложно, потому что я использую графический процессор.
Прилагается фрагмент кода, как я его настроил, и исходные строки кода помечены #py2
. Я был бы рад идеям и предложениям.
С уважением
# Python 3.9.7; GPU: yes (recognized); tf-version=2.4.1 import tensorflow as tf import os from tensorflow.python.ops import init_ops #from tensorflow.contrib.layers.python.layers import utils # py2 from tensorflow.keras import utils # py3 # from tensorflow.contrib.framework.python.ops import variables # py2 from tensorflow.python.ops import variables # py3 # ausprobiert from tensorflow.python.ops import variable_scope from tensorflow.python.training import moving_averages # BATCH_SIZE = 100; TRAINING_ITERS = 100; DATA_DIR = "data/"; TRAINING_INPUT_SIZE = 38; INPUT_DEPTH = 1; AUX_DEPTH = 1; LAYER_SIZES = [64, 32, 1]; OUTPUT_DEPTH = LAYER_SIZES[-1] lr_size = int(TRAINING_INPUT_SIZE / 2) hr_shape=[TRAINING_INPUT_SIZE, TRAINING_INPUT_SIZE] hr_d=OUTPUT_DEPTH DATA_DIR = 'scratch/ppt_016_032/' MODEL_NAME = 'ppt-016-032' # def inputs_climate(batch_size, num_epochs, data_dir, lr_d, aux_d, hr_d, lr_shape=None, hr_shape=None, is_training=False): filenames= sorted([os.path.join(data_dir, f) for f in os.listdir(data_dir) if 'tfrecords' in f]) if is_training: filenames = [f for f in filenames if 'train' in f] else: filenames = [f for f in filenames if 'test' in f] with tf.keras.backend.name_scope('input'), tf.device("/gpu:0"): # eV # replace as variable # cpu/gpu # versuch py3 #with tf.name_scope('input'), tf.device("/gpu:0"): # py2 # original # filename_queue = tf.train.string_input_producer(filenames) # py2 # original # filename_queue = tf.compat.v1.train.string_input_producer(filenames) # filename_queue = tf.data.TextLineDataset(filenames) filename_queue = tf.data.TFRecordDataset(filenames) #filename_queue = tf.data.Dataset.from_tensor_slices(filenames).shuffle(tf.shape(tf.name_scope('input'), out_type=tf.int64)[0]).repeat(num_epochs) print("tf_reader_inputs_climate_filename_queue: " str(filename_queue)) # eV print("tf_reader_inputs_climate_filename_queue_type: " str(type(filename_queue))) # eV data = read_and_decode(filename_queue, is_training, lr_d, aux_d, hr_d, lr_shape=lr_shape, hr_shape=hr_shape) 0/0 # with intention # [...] # more def read_and_decode(filename_queue, is_training,lr_d,aux_d,hr_d, lr_shape=None, hr_shape=None): #reader = tf.TFRecordReader() # py2 # original #reader = tf.compat.v1.TFRecordReader(None, None) #reader = tf.data.TFRecordDataset() reader = tf.data.TFRecordDataset(filename_queue) print("inputs_climate_read_and_decode_reader: " str(reader)) print("inputs_climate_read_and_decode_reader_type: " str(type(reader))) print("inputs_climate_read_and_decode_filename_queue: " str(filename_queue)) print("asdjkasndjidhjsadjksab") #_, serialized_example = reader.read(queue=filename_queue) # py2 # original serialized_example = tf.io.read_file(filename_queue) #serialized_example = reader.read(queue=filename_queue) print("se") print(serialized_example) print("basndkanjskdbjkasbksj") # features = tf.parse_single_example( # py2 # io ebenfalls fuer fixedlenfeature # py3 0/0 # with intention features = tf.io.parse_single_example(serialized_example,features={'hr_h': tf.io.FixedLenFeature([], tf.int64),'hr_w': tf.io.FixedLenFeature([], tf.int64),'lr_h': tf.io.FixedLenFeature([], tf.int64),'lr_w': tf.io.FixedLenFeature([], tf.int64),'label': tf.io.FixedLenFeature([], tf.string),'img_in': tf.io.FixedLenFeature([], tf.string),'aux': tf.io.FixedLenFeature([], tf.string),'lat': tf.io.FixedLenFeature([], tf.string),'lon': tf.io.FixedLenFeature([], tf.string),'time': tf.io.FixedLenFeature([], tf.int64)}) # [...] # more # function call: train_images, train_labels = inputs_climate(BATCH_SIZE, TRAINING_ITERS, DATA_DIR, lr_shape=[lr_size, lr_size], lr_d=INPUT_DEPTH, aux_d=AUX_DEPTH, is_training=True, hr_shape=[TRAINING_INPUT_SIZE, TRAINING_INPUT_SIZE], hr_d=OUTPUT_DEPTH)