我已经训练了以下Keras模型的子类。我希望能够在加载的模型上调用B_frame_CNN中的所有方法(例如,get_embedded())。以下代码运行得很好,并且在ipython笔记本上运行时可以满足我的需要。
import tensorflow as tf
class B_frame_CNN(tf.keras.Model):
def __init__(self, filter_size, activation, padding):
super().__init__()
self.c1 = tf.keras.layers.Conv2D(32, filter_size, strides=(2,1), activation=activation, padding=padding)
self.c2 = tf.keras.layers.Conv2D(64, filter_size, strides=2, activation=activation, padding=padding)
self.p1 = tf.keras.layers.MaxPooling2D(filter_size, strides=2)
self.c3 = tf.keras.layers.Conv2D(128, filter_size, strides=2, activation=activation, padding=padding, name="gradmaps")
self.p2 = tf.keras.layers.MaxPooling2D(filter_size, strides=2)
self.c4 = tf.keras.layers.Conv2D(256, filter_size, strides=2, activation=activation, padding=padding)
self.c5 = tf.keras.layers.Conv2D(256, filter_size, strides=2, activation=activation, padding=padding)
self.f1 = tf.keras.layers.Flatten()
self.dropout1 = tf.keras.layers.Dropout(0.5)
self.d1 = tf.keras.layers.Dense(64, activation=None, name="embedding")
self.dropout2 = tf.keras.layers.Dropout(0.5)
self.d2 = tf.keras.layers.Dense(2, activation='softmax')
inputs = tf.keras.Input(shape=(200,100,1))
self.full_model = tf.keras.Model(inputs=[inputs], outputs=self.call(inputs))
def call(self, inputs):
x = self.c1(inputs)
x = self.c2(x)
x = self.p1(x)
x = self.c3(x)
x = self.p2(x)
x = self.c4(x)
x = self.c5(x)
x = self.f1(x)
x = self.dropout1(x, training=True)
x = self.d1(x)
x = self.dropout2(x, training=True)
return self.d2(x)
def get_embedding(self):
intermediate = tf.keras.models.Model(inputs=self.full_model.input, outputs=self.full_model.get_layer("embedding").output)
return intermediate
model1 = B_frame_CNN(3, 'relu', 'same')
model1.load_weights("D:\\brain_cancer_oct\saved_models\CNN_bframe")
但是,当我在python脚本(.py)中运行它时,我得到了以下错误:
2022年09月10日01:44:08.287034:无法打开D:\脑肿瘤八位一体\保存的模型\CNN_bframe:未知:NewRandomAccessFile无法创建/打开:D:\脑癌_oct\保存的模型\CNN_bframe:访问被拒绝。输入/输出错误
我将感谢任何帮助破译这个错误。
1条答案
按热度按时间toiithl61#
如果有人遇到同样的问题,这里是我找到的变通办法:
我不能单独在bframe_model上使用我的类方法,但是一旦我将其权重加载到我示例化的BFrameCNN类的对象中,我就能够做我想做的事情(例如,调用get_intermediate()方法)。