我正在使用TensorFlow和CNTK设置和使用Keras来比较和学习这两个系统。我已经成功设置了TensorFlow,但是当我切换到CNTK时出现错误,我对如何解决它感到有些不知所措。看起来这个问题与Model命令有关,但如果我重新设计以适应CNTK方法,我将不得不保留我的代码的多个版本,这将击败Keras的一部分;有没有办法在Keras本地做这个与两者兼容?该代码是this的第三部分。
我收到了dll错误,如下所示:
C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\python.exe "C:/Users/shaff/LRZ Sync+Share/Deep Learning for Computer Vision/FinalProject/LearningKeras/KerasCatDog/KerasCatDogPart3.py"
Using CNTK backend
Traceback (most recent call last):
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\cntk\cntk_py.py", line 18, in swig_import_helper
return importlib.import_module(mname)
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'cntk._cntk_py'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/shaff/LRZ Sync+Share/Deep Learning for Computer Vision/FinalProject/LearningKeras/KerasCatDog/KerasCatDogPart3.py", line 39, in <module>
from keras import applications, Model
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\keras\__init__.py", line 3, in <module>
from . import utils
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
from . import conv_utils
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\keras\utils\conv_utils.py", line 3, in <module>
from .. import backend as K
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\keras\backend\__init__.py", line 77, in <module>
from .cntk_backend import *
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\keras\backend\cntk_backend.py", line 2, in <module>
import cntk as C
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\cntk\__init__.py", line 10, in <module>
from . import cntk_py
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\cntk\cntk_py.py", line 21, in <module>
_cntk_py = swig_import_helper()
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\cntk\cntk_py.py", line 20, in swig_import_helper
return importlib.import_module('_cntk_py')
File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed: The specified module could not be found.
Process finished with exit code 1
我正在运行的代码在这里,适用于TensorFlow:
from keras import applications, Model
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense
import time
# path to the model weights files.
top_model_weights_path = 'bottleneck_fc_model.h5'
# dimensions of our images.
img_width, img_height = 150, 150
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 2000
nb_validation_samples = 800
epochs = 50
batch_size = 16
# build the VGG16 network
base_model = applications.VGG16(include_top=False,weights='imagenet', input_shape=(img_height, img_width, 3))
print('Model loaded.')
# build a classifier model to put on top of the convolutional model
top_model = Sequential()
top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
# note that it is necessary to start with a fully-trained
# classifier, including the top classifier,
# in order to successfully do fine-tuning
top_model.load_weights(top_model_weights_path)
# add the model on top of the convolutional base
model = Model(input=base_model.input, output=top_model(base_model.output))
# set the first 15 layers (up to the last conv block)
# to non-trainable (weights will not be updated)
for layer in model.layers[:15]:
layer.trainable = False
# compile the model with a SGD/momentum optimizer
# and a very slow learning rate.
model.compile(loss='binary_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
# prepare data augmentation configuration
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='binary')
model.summary()
# fine-tune the model
t = time.time() # Track time
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size,
verbose=1)
print("Fit Generator Time:", time.time() - t) # Print Elapsed Time
我使用Python 3.6,Keras 2.1.2和CNTK 2.3.1在Windows 10上运行它。
答案 0 :(得分:0)
我知道我迟到了。但有两种解决方案可以解决此ModuleNotFoundError: No module named 'cntk._cntk_py'
错误。
1)提供CNTK .dll文件位置到系统变量的路径。 2)如果仍然无法解决问题,请下载并安装 Visual C ++ Redistributable for Visual Studio 2017 。