title: Keras CNTK Model Command Not Working (Works with TensorFlow)
body: <p>I'm in the process of setting up and using Keras with both TensorFlow and CNTK to compare and learn both systems. I've successfully set up the TensorFlow piece, but when I switch to CNTK I get an error and I'm a bit at a loss as to how to fix it. It looks like the issue is with the Model command but if I rework that to fit the CNTK approach I'll have to keep multiple versions of my code, which defeats part of the point of Keras; is there a way to do this natively in Keras to be compatible with both? The code is the third section of <a href="https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html" rel="nofollow noreferrer">this</a>.</p>
  
  <p>I'm getting a dll error, as shown here:</p>
  
  <pre><code>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 "&lt;frozen importlib._bootstrap&gt;", line 994, in _gcd_import
    File "&lt;frozen importlib._bootstrap&gt;", line 971, in _find_and_load
    File "&lt;frozen importlib._bootstrap&gt;", 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 &lt;module&gt;
      from keras import applications, Model
    File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\keras\__init__.py", line 3, in &lt;module&gt;
      from . import utils
    File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\keras\utils\__init__.py", line 6, in &lt;module&gt;
      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 &lt;module&gt;
      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 &lt;module&gt;
      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 &lt;module&gt;
      import cntk as C
    File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\cntk\__init__.py", line 10, in &lt;module&gt;
      from . import cntk_py
    File "C:\Users\shaff\AppData\Local\conda\conda\envs\KerasTensor\lib\site-packages\cntk\cntk_py.py", line 21, in &lt;module&gt;
      _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
  </code></pre>
  
  <p>The code I'm running is here and works fine for TensorFlow:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I'm running this on Windows 10 using Python 3.6, Keras 2.1.2, and CNTK 2.3.1.</p>