現在画像の1枚で(1,8,8,1256)の特徴量をもっていて一つの箱にそれが500枚あるので(500,8,8,1256)の配列を作りたいと考えています。私が考えたのが、appendを用いて配列を足し合わせることでしたが(500,1,8,8,1256)の配列となります。500の中の一つ一つに(8,8,1256)の特徴量を持つという意味で、下のコードでいうとimgsが(500,8,8,1256)の配列を作りたいと考えています。このようなコードにするには具体的にどのようにすればよいでしょうか。
#コード
def feature_create(load_img_paths,model,layer_name): imgs=[] center_layer_model = Model(inputs=model.input, outputs=model.get_layer(layer_name).output) for load_img_path in tqdm(load_img_paths): img = cv2.imread(load_img_path) target = np.reshape(img, (1, img.shape[0], img.shape[1], img.shape[2])).astype('float') / 255.0 center_output = center_layer_model.predict(target) imgs.append(center_output) return imgs
#エラーコード
Traceback (most recent call last): File "C:\Users\Desktop\resyuu.py", line 149, in <module> main() File "C:\Users\Desktop\resyuu.py", line 85, in main imgs1 = create_images_array(load_img1_paths,model,layer_name) File "C:\Users\Desktop\resyuu.py", line 28, in create_images_array middle_output = middle_layer_model.predict(target) File "C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py", line 130, in _method_wrapper return method(self, *args, **kwargs) File "C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1599, in predict tmp_batch_outputs = predict_function(iterator) File "C:\anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__ result = self._call(*args, **kwds) File "C:\anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 823, in _call self._initialize(args, kwds, add_initializers_to=initializers) File "C:\anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 697, in _initialize *args, **kwds)) File "C:\anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 2855, in _get_concrete_function_internal_garbage_collected graph_function, _, _ = self._maybe_define_function(args, kwargs) File "C:\anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 3213, in _maybe_define_function graph_function = self._create_graph_function(args, kwargs) File "C:\anaconda\lib\site-packages\tensorflow\python\eager\function.py", line 3075, in _create_graph_function capture_by_value=self._capture_by_value), File "C:\anaconda\lib\site-packages\tensorflow\python\framework\func_graph.py", line 986, in func_graph_from_py_func func_outputs = python_func(*func_args, **func_kwargs) File "C:\anaconda\lib\site-packages\tensorflow\python\eager\def_function.py", line 600, in wrapped_fn return weak_wrapped_fn().__wrapped__(*args, **kwds) File "C:\anaconda\lib\site-packages\tensorflow\python\framework\func_graph.py", line 973, in wrapper raise e.ag_error_metadata.to_exception(e) ValueError: in user code: C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:1462 predict_function * return step_function(self, iterator) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:1452 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) C:\anaconda\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) C:\anaconda\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) C:\anaconda\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica return fn(*args, **kwargs) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:1445 run_step ** outputs = model.predict_step(data) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\training.py:1418 predict_step return self(x, training=False) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:985 __call__ outputs = call_fn(inputs, *args, **kwargs) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\functional.py:386 call inputs, training=training, mask=mask) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\functional.py:508 _run_internal_graph outputs = node.layer(*args, **kwargs) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\base_layer.py:976 __call__ self.name) C:\anaconda\lib\site-packages\tensorflow\python\keras\engine\input_spec.py:180 assert_input_compatibility str(x.shape.as_list())) ValueError: Input 0 of layer conv1_pad is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 224, 3]
回答2件
あなたの回答
tips
プレビュー