Я пытаюсь объединить 2 предварительно подготовленные модели Keras, но не удалось - PullRequest
0 голосов
/ 11 января 2020

Моя цель состоит в том, чтобы использовать керас для загрузки «2 одинаковых» моделей предварительного обучения (re snet или начальное значение _net или что-то еще), выровнять их и добавить пару новых слоев для их обучения. Я думал, что это простая задача, но я просто не могу это сделать.

Я пробовал много разных способов, но компилятор постоянно говорит мне, что я допустил ошибку (1. Не могу сгладить 2. какой-то странный тензор отсутствует на графике и т. Д. c)

пожалуйста, помогите мне

(ps, я даже не могу сделать "импортировать тензор потока как тф", или модель покажет другую новую проблему)

это мой код:

from keras.models import Model
from keras.layers import Dense, concatenate
from keras.applications import vgg16
import numpy as np
import pandas as pd
import keras
from keras.layers import Dense, Flatten, Concatenate,Input,Conv2D
from keras.models import Model
#import tensorflow as tf
from tensorflow import reset_default_graph
from keras.layers import Dense, concatenate
from keras.applications import vgg16
import numpy as np
import pandas as pd
import keras
from keras.layers import Dense, Flatten, Concatenate,Input,Dropout
from keras.models import Model
#import tensorflow as tf
from tensorflow import reset_default_graph
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import tensorflow as tf
reset_default_graph()
#vgg16_model =vgg16. VGG16(weights="imagenet", include_top=False, input_shape=(224,224,3))
#x = vgg16_model.output

resnet50_model =ResNet50(weights="imagenet", include_top=False, input_shape=(224,224,3))
x = resnet50_model.output

x=Flatten()(x)
x=Dense(256, activation='relu')(x)
x=Dropout(0.5)(x)
predictions=Dense(3, activation='softmax')(x)

#model_1 = Model(inputs=vgg16_model.input, outputs=predictions)
model_1 = Model(inputs=resnet50_model.input, outputs=predictions)

#vgg16_model =vgg16. VGG16(weights="imagenet", include_top=False, input_shape=(224,224,3))
#x = vgg16_model.output
resnet50_model =ResNet50(weights="imagenet", include_top=False, input_shape=(224,224,3))
x = resnet50_model.output

x=Flatten()(x)
x=Dense(256, activation='relu')(x)
x=Dropout(0.5)(x)
predictions=Dense(3, activation='softmax')(x)

#model_2 = Model(inputs=vgg16_model.input, outputs=predictions)
model_2 = Model(inputs=resnet50_model.input, outputs=predictions)

merged = concatenate([model_1.output, model_2.output])
#merged=Conv2D(64, (3, 3))(merged)
merged=Flatten()(merged)
merged=Dropout(0.5)(merged)
merged = Dense(1024, activation='relu')(merged)
merged = Dense(num_classes, activation='softmax')(merged)

model_fusion = Model([model_1.input, model_2.input], merged)
model_fusion.compile('adam', loss = 'binary_crossentropy')
model_fusion.summary()

это сообщение об ошибке

ValueError                                Traceback (most recent call last)
<ipython-input-1-c22d3213d917> in <module>
     52 merged = concatenate([model_1.output, model_2.output])
     53 #merged=Conv2D(64, (3, 3))(merged)
---> 54 merged=Flatten()(merged)
     55 merged=Dropout(0.5)(merged)
     56 merged = Dense(1024, activation='relu')(merged)

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
    412                 # Raise exceptions in case the input is not compatible
    413                 # with the input_spec specified in the layer constructor.
--> 414                 self.assert_input_compatibility(inputs)
    415 
    416                 # Collect input shapes to build layer.

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\engine\base_layer.py in assert_input_compatibility(self, inputs)
    325                                      self.name + ': expected min_ndim=' +
    326                                      str(spec.min_ndim) + ', found ndim=' +
--> 327                                      str(K.ndim(x)))
    328             # Check dtype.
    329             if spec.dtype is not None:

ValueError: Input 0 is incompatible with layer flatten_3: expected min_ndim=3, found ndim=2

это предупреждающее сообщение:

Using TensorFlow backend.
WARNING: Logging before flag parsing goes to stderr.
W0112 07:22:12.336960 14720 deprecation_wrapper.py:119] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

W0112 07:22:12.351963 14720 deprecation_wrapper.py:119] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.

W0112 07:22:12.356964 14720 deprecation_wrapper.py:119] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:4185: The name tf.truncated_normal is deprecated. Please use tf.random.truncated_normal instead.

W0112 07:22:12.376968 14720 deprecation_wrapper.py:119] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:174: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.

W0112 07:22:12.378969 14720 deprecation_wrapper.py:119] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:181: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.

W0112 07:22:12.408976 14720 deprecation_wrapper.py:119] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:1834: The name tf.nn.fused_batch_norm is deprecated. Please use tf.compat.v1.nn.fused_batch_norm instead.

W0112 07:22:12.460988 14720 deprecation_wrapper.py:119] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:3976: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras_applications\resnet50.py:265: UserWarning: The output shape of `ResNet50(include_top=False)` has been changed since Keras 2.2.0.
  warnings.warn('The output shape of `ResNet50(include_top=False)` '
W0112 07:22:18.834421 14720 deprecation.py:506] From C:\Users\User\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.

Кстати, если я попытаюсь снова выполнить код, появится следующее сообщение:

короткая версия:

TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.

длинная версия:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1112             subfeed_t = self.graph.as_graph_element(
-> 1113                 subfeed, allow_tensor=True, allow_operation=False)
   1114           except Exception as e:

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\tensorflow\python\framework\ops.py in as_graph_element(self, obj, allow_tensor, allow_operation)
   3795     with self._lock:
-> 3796       return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
   3797 

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\tensorflow\python\framework\ops.py in _as_graph_element_locked(self, obj, allow_tensor, allow_operation)
   3874       if obj.graph is not self:
-> 3875         raise ValueError("Tensor %s is not an element of this graph." % obj)
   3876       return obj

ValueError: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-c22d3213d917> in <module>
     26 #x = vgg16_model.output
     27 
---> 28 resnet50_model =ResNet50(weights="imagenet", include_top=False, input_shape=(224,224,3))
     29 x = resnet50_model.output
     30 

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\applications\__init__.py in wrapper(*args, **kwargs)
     26             kwargs['models'] = models
     27             kwargs['utils'] = utils
---> 28         return base_fun(*args, **kwargs)
     29 
     30     return wrapper

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\applications\resnet50.py in ResNet50(*args, **kwargs)
      9 @keras_modules_injection
     10 def ResNet50(*args, **kwargs):
---> 11     return resnet50.ResNet50(*args, **kwargs)
     12 
     13 

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras_applications\resnet50.py in ResNet50(include_top, weights, input_tensor, input_shape, pooling, classes, **kwargs)
    289                 cache_subdir='models',
    290                 md5_hash='a268eb855778b3df3c7506639542a6af')
--> 291         model.load_weights(weights_path)
    292         if backend.backend() == 'theano':
    293             keras_utils.convert_all_kernels_in_model(model)

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\engine\network.py in load_weights(self, filepath, by_name, skip_mismatch, reshape)
   1164             else:
   1165                 saving.load_weights_from_hdf5_group(
-> 1166                     f, self.layers, reshape=reshape)
   1167 
   1168     def _updated_config(self):

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\engine\saving.py in load_weights_from_hdf5_group(f, layers, reshape)
   1056                              ' elements.')
   1057         weight_value_tuples += zip(symbolic_weights, weight_values)
-> 1058     K.batch_set_value(weight_value_tuples)
   1059 
   1060 

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\keras\backend\tensorflow_backend.py in batch_set_value(tuples)
   2468             assign_ops.append(assign_op)
   2469             feed_dict[assign_placeholder] = value
-> 2470         get_session().run(assign_ops, feed_dict=feed_dict)
   2471 
   2472 

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    948     try:
    949       result = self._run(None, fetches, feed_dict, options_ptr,
--> 950                          run_metadata_ptr)
    951       if run_metadata:
    952         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\Anaconda3\envs\tensorflow-gpu2\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1114           except Exception as e:
   1115             raise TypeError(
-> 1116                 'Cannot interpret feed_dict key as Tensor: ' + e.args[0])
   1117 
   1118           if isinstance(subfeed_val, ops.Tensor):

TypeError: Cannot interpret feed_dict key as Tensor: Tensor Tensor("Placeholder:0", shape=(7, 7, 3, 64), dtype=float32) is not an element of this graph.

большое спасибо за чтение параграфа.

1 Ответ

1 голос
/ 12 января 2020

Я сделал несколько незначительных правок. Изменены операторы импорта, чтобы использовать tenorflow keras. Использовал VGG вместо Re sNet, чтобы избежать конфликтов имен слоев. Дали произвольное значение для num_classes. Но я не уверен, что архитектура верна, хотя весь код работает нормально.

#Edited imports to have TF keras instead of keras.
import tensorflow
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, concatenate
from tensorflow.keras.applications import vgg16
import numpy as np
#import pandas as pd
#import keras
from tensorflow.keras.layers import Dense, Flatten, Concatenate,Input,Conv2D
from tensorflow.keras.models import Model
#import tensorflow as tf
from tensorflow import reset_default_graph
from tensorflow.keras.layers import Dense, concatenate
from tensorflow.keras.applications import vgg16
#import numpy as np
#import pandas as pd
#import keras
from tensorflow.keras.layers import Dense, Flatten, Concatenate,Input,Dropout
from tensorflow.keras.models import Model
#import tensorflow as tf
from tensorflow import reset_default_graph
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
#import tensorflow as tf
#reset_default_graph()
#vgg16_model =vgg16. VGG16(weights="imagenet", include_top=False, input_shape=(224,224,3))
#x = vgg16_model.output

resnet50_model =ResNet50(weights="imagenet", include_top=False, input_shape=(224,224,3))
x = resnet50_model.output

x=Flatten()(x)
x=Dense(256, activation='relu')(x)
x=Dropout(0.5)(x)
predictions=Dense(3, activation='softmax')(x)

#model_1 = Model(inputs=vgg16_model.input, outputs=predictions)
model_1 = Model(inputs=resnet50_model.input, outputs=predictions)

vgg16_model =vgg16. VGG16(weights="imagenet", include_top=False, input_shape=(224,224,3))#Edited here.Using same model causes name conflicts of layers.Every layer names should be unique.
x = vgg16_model.output
#resnet50_model =ResNet50(weights="imagenet", include_top=False, input_shape=(224,224,3))
#x = resnet50_model.output

x=Flatten()(x)
x=Dense(256, activation='relu')(x)
x=Dropout(0.5)(x)
predictions=Dense(3, activation='softmax')(x)

model_2 = Model(inputs=vgg16_model.input, outputs=predictions)
#model_2 = Model(inputs=resnet50_model.input, outputs=predictions)

merged = concatenate([model_1.output, model_2.output])
print(merged)
#merged=Conv2D(64, (3, 3))(merged)
merged=Flatten()(merged)
merged=Dropout(0.5)(merged)
merged = Dense(1024, activation='relu')(merged)
num_classes=3#Edited here added num_classes explicitly.
merged = Dense(num_classes, activation='softmax')(merged)

model_fusion = Model([model_1.input, model_2.input], merged)
model_fusion.compile('adam', loss = 'binary_crossentropy')
model_fusion.summary()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...