geoffwoollard / ece1512_project

0 stars 0 forks source link

save model #17

Open geoffwoollard opened 5 years ago

geoffwoollard commented 5 years ago

See https://machinelearningmastery.com/how-to-make-classification-and-regression-predictions-for-deep-learning-models-in-keras/ and https://machinelearningmastery.com/save-load-keras-deep-learning-models/

Davjes15 commented 5 years ago

@geoffwoollard I am having an issue trying to save the model as a JSON file. image

Do you mind if I save it in a different format such as hdf5?

geoffwoollard commented 5 years ago

I can load and predict if saved as yaml/hd5, but this causes problems with trying to modify this model to visualize

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-70-8358fa10b0c8> in <module>()
      1 
      2 ii=1
----> 3 img = visualize.visualize_saliency_wrapper(loaded_model, seed_input=X_val[ii,:,:,:])
      4 plt.subplot(121)
      5 plt.imshow(X_val[ii,:,:,0])

/content/drive/My Drive/ece1512/project/ece1512_project/visualize.py in visualize_saliency_wrapper(model, seed_input)
     23 
     24 def visualize_saliency_wrapper(model,seed_input):
---> 25         model = prep_model_output(model)
     26         img = visualize_saliency(model=model, layer_idx=-1, filter_indices=0, seed_input=seed_input)
     27         return(img)

/content/drive/My Drive/ece1512/project/ece1512_project/visualize.py in prep_model_output(model)
     12         # Swap softmax with linear
     13         model.layers[layer_idx].activation = activations.linear
---> 14         model = utils.apply_modifications(model)
     15 
     16         # This is the output node we want to maximize.

/usr/local/lib/python3.6/dist-packages/vis/utils/utils.py in apply_modifications(model, custom_objects)
    110     model_path = os.path.join(tempfile.gettempdir(), next(tempfile._get_candidate_names()) + '.h5')
    111     try:
--> 112         model.save(model_path)
    113         return load_model(model_path, custom_objects=custom_objects)
    114     finally:

/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in save(self, filepath, overwrite, include_optimizer)
   1088             raise NotImplementedError
   1089         from ..models import save_model
-> 1090         save_model(self, filepath, overwrite, include_optimizer)
   1091 
   1092     def save_weights(self, filepath, overwrite=True):

/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in save_model(model, filepath, overwrite, include_optimizer)
    380 
    381     try:
--> 382         _serialize_model(model, f, include_optimizer)
    383     finally:
    384         if opened_new_file:

/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in _serialize_model(model, f, include_optimizer)
     82     model_config['class_name'] = model.__class__.__name__
     83     model_config['config'] = model.get_config()
---> 84     model_config = json.dumps(model_config, default=get_json_type)
     85     model_config = model_config.encode('utf-8')
     86     f['model_config'] = model_config

/usr/lib/python3.6/json/__init__.py in dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    236         check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    237         separators=separators, default=default, sort_keys=sort_keys,
--> 238         **kw).encode(obj)
    239 
    240 

/usr/lib/python3.6/json/encoder.py in encode(self, o)
    197         # exceptions aren't as detailed.  The list call should be roughly
    198         # equivalent to the PySequence_Fast that ''.join() would do.
--> 199         chunks = self.iterencode(o, _one_shot=True)
    200         if not isinstance(chunks, (list, tuple)):
    201             chunks = list(chunks)

/usr/lib/python3.6/json/encoder.py in iterencode(self, o, _one_shot)
    255                 self.key_separator, self.item_separator, self.sort_keys,
    256                 self.skipkeys, _one_shot)
--> 257         return _iterencode(o, 0)
    258 
    259 def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,

/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in get_json_type(obj)
     72             return obj.__name__
     73 
---> 74         raise TypeError('Not JSON Serializable: %s' % (obj,))
     75 
     76     from .. import __version__ as keras_version

TypeError: Not JSON Serializable: 190