python - tensorflow TypeError: float() argument must be a string or a number, not 'dict' -
i have problem. try execute code got error.
import matplotlib.pyplot plt # visualizations shown in notebook. %matplotlib inline import glob import cv2 import numpy np def plot_figures(figures, nrows, ncols, labels=none): fig, axs = plt.subplots(ncols=ncols, nrows=nrows, figsize=(12, 14)) axs = axs.ravel() index, title in zip(range(len(figures)), figures): axs[index].imshow(figures[title], plt.gray()) if(labels != none): axs[index].set_title(labels[index]) else: axs[index].set_title(title) axs[index].set_axis_off() plt.tight_layout() ### visualize network's feature maps here. ### feel free use many code cells needed. # image_input: test image being fed network produce feature maps # tf_activation: should tf variable name used during training procedure represents calculated state of specific weight layer # activation_min/max: can used view activation contrast in more detail, default matplot sets min , max actual min , max values of output # plt_num: used plot out multiple different weight feature map sets on same block, extend plt number each new feature map entry def outputfeaturemap(image_input, tf_activation, activation_min=-1, activation_max=-1 ,plt_num=1): # here make sure preprocess image_input in way network expects # size, normalization, ect if needed # image_input = # note: x should same name network's tensorflow data placeholder variable # if error tf_activation not defined maybe having trouble accessing variable inside function activation = tf_activation.eval(session=sess,feed_dict={x : image_input}) featuremaps = activation.shape[3] plt.figure(plt_num, figsize=(15,15)) featuremap in range(featuremaps): plt.subplot(6,8, featuremap+1) # sets number of feature maps show on each row , column plt.title('featuremap ' + str(featuremap)) # displays feature map number if activation_min != -1 & activation_max != -1: plt.imshow(activation[0,:,:, featuremap], interpolation="nearest", vmin =activation_min, vmax=activation_max, cmap="gray") elif activation_max != -1: plt.imshow(activation[0,:,:, featuremap], interpolation="nearest", vmax=activation_max, cmap="gray") elif activation_min !=-1: plt.imshow(activation[0,:,:, featuremap], interpolation="nearest", vmin=activation_min, cmap="gray") else: plt.imshow(activation[0,:,:, featuremap], interpolation="nearest", cmap="gray") my_images = sorted(glob.glob('./traffic_images/*.jpg')) my_labels = np.array([1, 28, 17, 10, 33]) name_values = np.genfromtxt('signnames.csv', skip_header=1, dtype=[('myint','i8'), ('mysring','s55')], delimiter=',') figures = {} labels = {} my_signs = [] index = 0 my_image in my_images: img = cv2.cvtcolor(cv2.imread(my_image), cv2.color_bgr2rgb) my_signs.append(img) figures[index] = img labels[index] = name_values[my_labels[index]][1].decode('ascii') index += 1 plot_figures(figures, 5, 1, labels) ### run predictions here , use model output prediction each image. ### make sure pre-process images same pre-processing pipeline used earlier. ### feel free use many code cells needed. my_signs = np.array(my_signs) tf.session() sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, "./lenet") my_accuracy = evaluate(my_signs, my_labels) print("my data set accuracy = {:.3f}".format(my_accuracy)) ### calculate accuracy these 5 new images. ### example, if model predicted 1 out of 5 signs correctly, it's 20% accurate on these new images. my_single_item_array = [] my_single_item_label_array = [] = 0 in range(5): my_single_item_array.append(my_signs[i]) my_single_item_label_array.append(my_labels[i]) tf.session() sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, "./lenet") my_accuracy = evaluate(my_single_item_array, my_single_item_label_array) print('image {}'.format(i+1)) print("image accuracy = {:.3f}".format(my_accuracy)) print() k_size = 5 softmax_logits = tf.nn.softmax(logits) top_k = tf.nn.top_k(softmax_logits, k=k_size) tf.session() sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, "./lenet") my_softmax_logits = sess.run(softmax_logits, feed_dict={x: my_signs}) my_top_k = sess.run(top_k, feed_dict={x: my_signs}) in range(5): figures = {} labels = {} figures[0] = my_signs[i] labels[0] = "original" j in range(k_size): labels[j+1] = 'guess {} : ({:.0f}%)'.format(j+1, 100*my_top_k[0][i][j]) figures[j+1] = x_valid[np.argwhere(y_valid == my_top_k[1][i][j])[0]].squeeze() plot_figures(figures, 1, 6, labels) ymax = figures[0].max() ymin = figures[0].min() outputfeaturemap(image_input=figures, tf_activation=softmax_logits, activation_min=ymin, activation_max=ymax , plt_num=k_size)
but got error message
--------------------------------------------------------------------------- typeerror traceback (most recent call last) <ipython-input-22-58bf65741b75> in <module>() 122 ymax = figures[0].max() 123 ymin = figures[0].min() --> 124 outputfeaturemap(image_input=figures, tf_activation=softmax_logits, activation_min=ymin, activation_max=ymax , plt_num=k_size) 125 126 <ipython-input-22-58bf65741b75> in outputfeaturemap(image_input, tf_activation, activation_min, activation_max, plt_num) 38 # note: x should same name network's tensorflow data placeholder variable 39 # if error tf_activation not defined maybe having trouble accessing variable inside function ---> 40 activation = tf_activation.eval(session=sess,feed_dict={x : image_input}) 41 featuremaps = activation.shape[3] 42 plt.figure(plt_num, figsize=(15,15)) /home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in eval(self, feed_dict, session) 573 574 """ --> 575 return _eval_using_default_session(self, feed_dict, self.graph, session) 576 577 /home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in _eval_using_default_session(tensors, feed_dict, graph, session) 3631 "the tensor's graph different session's " 3632 "graph.") -> 3633 return session.run(tensors, feed_dict) 3634 3635 /home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata) 764 try: 765 result = self._run(none, fetches, feed_dict, options_ptr, --> 766 run_metadata_ptr) 767 if run_metadata: 768 proto_data = tf_session.tf_getbuffer(run_metadata_ptr) /home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata) 935 ' larger type (e.g. int64).') 936 --> 937 np_val = np.asarray(subfeed_val, dtype=subfeed_dtype) 938 939 if not subfeed_t.get_shape().is_compatible_with(np_val.shape): /home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/numpy/core/numeric.py in asarray(a, dtype, order) 480 481 """ --> 482 return array(a, dtype, copy=false, order=order) 483 484 def asanyarray(a, dtype=none, order=none): typeerror: float() argument must string or number, not 'dict'
i not sure if problem tf_activation=softmax_logits
or if else. have idea???
Comments
Post a Comment