def tower_loss(data_tensor, label_tensor, num_classes, train_mode): with slim.arg_scope(my_vgg_relu6_fc.vgg_arg_scope(weight_decay=args.weight_decay)): logits, endpoints_dict = my_vgg_relu6_fc.vgg_16_tcomb(data_tensor, num_classes=num_classes, is_training=train_mode,dropout_keep_prob=args.dropout_keep_prob) loss=tf.reduce_mean(tf.losses.sparse_softmax_cross_entropy(labels=label_tensor, logits=logits)) return loss, logits
def main(args): pdb.set_trace() #modelDir = '/trn_dir/models/model1' model_path = '/trn_dir/models/model3_relu6/model-000039' num_classes=20 im0 = Image.open('/trn_dir/imgnet/data/trn/whippet/n02091134_12142.JPEG') im0=im0.resize((224,224)) image_np = load_image_into_numpy_array(im0) image_np_expanded = np.expand_dims(image_np, axis=0) vgg16_variable_names, vgg16_t_variable_names = my_get_variable_names() #builder = tf.saved_model.builder.SavedModelBuilder(modelDir) # -------------------------------------------------------------------------- # In TensorFlow, you first want to define the computation graph with all the # necessary operations: loss, training op, accuracy... # Any tensor created in the `graph.as_default()` scope will be part of `graph` graph = tf.Graph() with graph.as_default(): # Indicates whether we are in training or in test mode is_training = tf.placeholder(tf.bool) images = tf.placeholder(dtype=tf.float32, shape=[None,224,224,3]) res = layers.conv2d(images, 64, [3,3], scope='tmp') ninp_ch = tf.shape(res)[3] chidx = tf.range(0,tf.cast(ninp_ch/2,tf.int32)) rndidx = tf.random.shuffle(chidx) res2 = tf.gather(res, chidx, axis=3) #res[:,:,:,rndidx,:] res2.set_shape([None,res.shape[1],res.shape[2], res.shape[3].__floordiv__(2)] ) res1 = layers.conv2d(res2, 128, [3,3], scope='tmp_2c') chidx1 = tf.range(ninp_ch) chidx1_shift = tf.mod(chidx1+1,ninp_ch) #vgg = tf.contrib.slim.nets.vgg with slim.arg_scope(my_vgg_relu6_fc.vgg_arg_scope(weight_decay=args.weight_decay)): logits, endpoints_dict = my_vgg_relu6_fc.vgg_16(images, num_classes=num_classes, is_training=is_training,dropout_keep_prob=args.dropout_keep_prob) with slim.arg_scope(my_vgg_relu6_fc.vgg_arg_scope(weight_decay=args.weight_decay)): logits_t, endpoints_dict_t = my_vgg_relu6_fc.vgg_16_t(images, num_classes=num_classes, is_training=is_training,dropout_keep_prob=args.dropout_keep_prob) # Specify where the model checkpoint is (pretrained weights). #model_path = args.model_path #assert(os.path.isfile(model_path)) # Restore only the layers up to fc7 (included) # Calling function `init_fn(sess)` will load all the pretrained weights. main_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='vgg_16') #target_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='vgg_16_t') #variables_to_restore = tf.contrib.framework.get_variables_to_restore()#exclude=['vgg_16/fc8']) vgg_trainable_names = tf.trainable_variables(scope='vgg_16') vgg_t_trainable_names = tf.trainable_variables('vgg_16_t') variables_to_restore = tf.contrib.framework.get_variables_to_restore(vgg16_variable_names) var_load_fn = tf.contrib.framework.assign_from_checkpoint_fn(model_path, variables_to_restore) #new_variables = tf.contrib.framework.get_variables('vgg_16_ext') #new_var_init = tf.variables_initializer(var_list=fc8_variables) copy_op = copy_values_from_vgg16(vgg16_variable_names, vgg16_t_variable_names) conv_weights = tf.contrib.framework.get_variables('vgg_16/conv2/conv2_1/weights:0') zero_weights = tf.zeros_like(conv_weights, dtype=conv_weights[0].dtype) ext_weights = tf.concat([conv_weights, zero_weights], axis=3) c1_1w=tf.contrib.framework.get_variables('vgg_16/conv1/conv1_1/biases:0') tc1_1w=tf.contrib.framework.get_variables('vgg_16_t/conv1/conv1_1/biases:0') assign_op = tf.assign(tc1_1w[0],tf.identity(c1_1w[0])) idx = tf.range(0,15) randidx = tf.random.shuffle(idx) ''' conv_weights = tf.contrib.framework.get_variables('vgg_16_ext/conv2/conv2_1/weights:0') conv_input_dim = tf.shape(conv_weights)[3] new_weights = tf.contrib.framework.get_variables('vgg_16_ext/conv2/conv2_1/weights:0') ext_weights = tf.concat([conv_weights, new_weights[:,:,:,conv_input_dim:,:]], axis=3) ''' ''' ''' # Initialization operation from scratch for the new "fc8" layers # `get_variables` will only return the variables whose name starts with the given pattern global_init=tf.global_variables_initializer() with tf.Session(graph=graph) as sess: pdb.set_trace() #sess.run(new_var_init) sess.run(global_init) var_load_fn(sess) # load the pretrained weights # print name, shape of each trainable variable trainable_values = sess.run(vgg_trainable_names) for nm, v in zip(vgg_trainable_names, trainable_values): print('name = ',nm.name, ' shape = ',v.shape) conv_val = sess.run(conv_weights, {images:image_np_expanded, is_training:True}) ext_val = sess.run(ext_weights, {images:image_np_expanded, is_training:True}) randidx = sess.run(randidx, {images:image_np_expanded, is_training:True}) pp=1
def main(args): pdb.set_trace() #modelDir = '/trn_dir/models/model1' #model_path = '/trn_dir/models/model_multi_cl20_relu6/model-000075' model_path = '/trn_dir/models/model_multi_cl20_preprocess/model-000060' num_classes = 20 im0 = Image.open('/trn_dir/imgnet/data/trn/whippet/n02091134_12142.JPEG') im0 = im0.resize((224, 224)) image_np = load_image_into_numpy_array(im0) image_np_expanded = np.expand_dims(image_np, axis=0) vgg16_variable_names, vgg16_t_variable_names = my_get_variable_names() #builder = tf.saved_model.builder.SavedModelBuilder(modelDir) # -------------------------------------------------------------------------- # In TensorFlow, you first want to define the computation graph with all the # necessary operations: loss, training op, accuracy... # Any tensor created in the `graph.as_default()` scope will be part of `graph` graph = tf.Graph() with graph.as_default(): # Indicates whether we are in training or in test mode is_training = tf.placeholder(tf.bool) images = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3]) #vgg = tf.contrib.slim.nets.vgg with slim.arg_scope( my_vgg_relu6_fc.vgg_arg_scope(weight_decay=args.weight_decay)): logits, endpoints_dict = my_vgg_relu6_fc.vgg_16( images, num_classes=num_classes, is_training=is_training, dropout_keep_prob=args.dropout_keep_prob) main_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='vgg_16') #target_variables = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='vgg_16_t') #variables_to_restore = tf.contrib.framework.get_variables_to_restore()#exclude=['vgg_16/fc8']) vgg_trainable_names = tf.trainable_variables(scope='vgg_16') variables_to_restore = tf.contrib.framework.get_variables_to_restore( vgg16_variable_names) var_load_fn = tf.contrib.framework.assign_from_checkpoint_fn( model_path, variables_to_restore) global_init = tf.global_variables_initializer() with tf.Session(graph=graph) as sess: pdb.set_trace() #sess.run(new_var_init) sess.run(global_init) var_load_fn(sess) # load the pretrained weights # print name, shape of each trainable variable savename = os.path.basename(model_path) + '_featcorr.json' vgg_variable_values = {} feat_corr = {} trainable_values = sess.run(vgg_trainable_names) for nm, v in zip(vgg_trainable_names[:44], trainable_values[:44]): print('name = ', nm.name, ' shape = ', v.shape) if 'vgg_16_tcorr' not in nm.name: vgg_variable_values[nm.name] = np.array(v) for vname in vgg16_variable_names: print(vname) wname = vname + '/weights:0' bname = vname + '/biases:0' feat_corr[vname] = compute_feature_correlation( vgg_variable_values[wname], vgg_variable_values[bname]).tolist() with open(savename, 'w') as fid: json.dump(feat_corr, fid) fid.close() #feat_corr[nm.name] = compute_feature_correlation(vgg_variable_values[nm.name]) #feat_corr = compute_feature_correlation(vgg_variable_values['vgg_16/conv3/conv3_2/weights:0']) pp = 1