I am in the process of converting this code predictor.py located here to tensorflow v2 with Python 3.7. I want to do this conversion part by part. So from the refence repo, I've been going through this example.

For this block:

def resBlock(x, num_outputs, kernel_size = 4, stride=1, activation_fn=tf.nn.relu, normalizer_fn=tcl.batch_norm, scope=None):
    assert num_outputs%2==0 #num_outputs must be divided by channel_factor(2 here)
    with tf.variable_scope(scope, 'resBlock'):
        shortcut = x
        if stride != 1 or x.get_shape()[3] != num_outputs:
            shortcut = tcl.conv2d(shortcut, num_outputs, kernel_size=1, stride=stride, 
                        activation_fn=None, normalizer_fn=None, scope='shortcut')
        x = tcl.conv2d(x, num_outputs/2, kernel_size=1, stride=1, padding='SAME')
        x = tcl.conv2d(x, num_outputs/2, kernel_size=kernel_size, stride=stride, padding='SAME')
        x = tcl.conv2d(x, num_outputs, kernel_size=1, stride=1, activation_fn=None, padding='SAME', normalizer_fn=None)

        x += shortcut       
        x = normalizer_fn(x)
        x = activation_fn(x)
    return x

Here is what I have

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Activation, BatchNormalization
from tensorflow.keras.layers import Conv2D, Conv2DTranspose
from tensorflow.keras.models import Sequential
from tensorflow.keras.regularizers import l2

def create_sub_model(

  assert (filters % 2 == 0), __file__ + ":filters is not an integer"
  name_base = 'conv' + str(stage) + block + '_branch'
  model = Sequential()
  #initial layer
  model.add(Conv2D(filters / 2, kernel_size=1, stride=(1, 1), padding='same',
                   name=conv_name_base + '2a'))
  model.add(BatchNormalization(name=bn_name_base + '2a'))
  #add layer
  model.add(Conv2D(filters / 2, kernel_size=kernel_size, stride=stride, padding='same',
                   name=conv_name_base + '2b'))
  model.add(BatchNormalization(name=bn_name_base + '2b'))
  # add layer
  model.add(Conv2D(filters, kernel_size=1, stride=(1,1), padding='same',
                   name=conv_name_base + '2c',activation=None, normalizer=None))
  model.add(BatchNormalization(name=bn_name_base + '2c'))
  # add layer
  model.add(Conv2D(filters, kernel_size=1, stride=stride, padding='same',
                   name=conv_name_base + '2d', activation=None, normalizer=None))
  model.add(BatchNormalization(normalizername=bn_name_base + '2d'))
  return model

I want to avoid defining the input tensor until later. Is this going down the right way?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.