Giter Club home page Giter Club logo

f-clswgan's Introduction

Feature Generating Networks for ZSL in Keras

N|Solid

keras implementation of paper: Feature Generating Networks for Zero-Shot Learning

Datasets

Original files

Download: data This dataset provides a platform to benchmark transfer-learning algorithms, in particular, attribute base classification and zero-shot learning. It can act as a drop-in replacement to the original Animals with Attributes (AwA) dataset, as it has the same class structure and almost the same characteristics. In addition, on this website, you will find CUB, SUN, FLO.

Dataset in H5File

In order to run our experiments, and facilitate visualization of the dataset, we introduce the GZSL datasets in H5file format. You can download the datasets CUB, SUN, FLO, and AWA1 in the h5file format, which can be downloaded dataset.zip (~3GB in zip file).

f-clswgan's People

Contributors

shayanramazi avatar

Stargazers

 avatar

Watchers

 avatar

f-clswgan's Issues

Report of Possible Errors

Hello,

I ran into some errors while running the code, which are summarized below:

#1 from keras.layers.merge import _Merge ImportError: cannot import name '_Merge' from 'keras.layers.merge' python

Since Merge is not supported in Keras +2, I have made some modifications to the RandomWeightedAverage() class:

Old code:

from keras.layers.merge import _Merge
import keras.backend as K

class RandomWeightedAverage(_Merge):
    """Provides a (random) weighted average between real and generated image samples"""
    def _merge_function(self, inputs):
        alpha = K.random_uniform((1024, 1))
        return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])

Funtional code:

import keras.backend as K
import tensorflow as tf

class RandomWeightedAverage(tf.keras.layers.Layer):
    def call(self, inputs, **kwargs):
        alpha = K.random_uniform((1024, 1))
        return (alpha * inputs[0]) + ((1 - alpha) * inputs[1])

Then, you need to do a little change in line 48 of the CLSWGAN.py file:

FROM

interpolated_features = RandomWeightedAverage()([real_features, fake_features])

TO

interpolated_features = RandomWeightedAverage()(inputs=[real_features, fake_features])

#2 AttributeError: Can't set the attribute "name"

Another error is related to the attribute "name" in line 69 of the CLSWGAN.py file. The solution is as simple as add a "_" before the attribute "name":

classificationLayer._name = 'modelClassifier'

#3 ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported

This error was mentioned in the jacobgil/keras-grad-cam#17 (comment) issue, and appears to have been fixed by adding a piece of code to the LossFunctions.py file, namely the _compute_gradients(tensor, var_list) function and a small change to line 15.

import tensorflow as tf

def _compute_gradients(tensor, var_list):
    grads = tf.gradients(tensor, var_list)
    return [grad if grad is not None else tf.zeros_like(var) for var, grad in zip(var_list, grads)]

def gradient_penalty_loss(y_true, y_pred, averaged_samples):
    """
    Computes gradient penalty based on prediction and weighted real / fake samples
    """
    gradients = _compute_gradients(y_pred, [averaged_samples])[0]
    (...)

Error while Running test.py and clswgan.py file

Helle @ShayanRamazi, While running the test.py file I am facing an error, can you help me with this

error:
Traceback (most recent call last):
File "c:\kush\BTP\f-CLSWGAN\test.py", line 3, in
wgan.train(epochs=30000, batch_size=1024, sample_interval=10)
File "c:\kush\BTP\f-CLSWGAN\CLSWGAN.py", line 115, in train
d_loss = self.critic_model.train_on_batch([features, labels, noise], [valid, fake, dummy])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\training.py", line 2787, in train_on_batch
logs = self.train_function(iterator)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\priya\AppData\Local\Temp_autograph_generated_filep8p_9d2q.py", line 15, in tf__train_function
retval
= ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\training.py", line 1384, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\training.py", line 1373, in run_step
outputs = model.train_step(data)
^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\training.py", line 1151, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\training.py", line 1209, in compute_loss
return self.compiled_loss(
^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\compile_utils.py", line 277, in call
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\losses.py", line 143, in call
losses = call_fn(y_true, y_pred)
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\losses.py", line 270, in call
return ag_fn(y_true, y_pred, **self.fn_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\priya\AppData\Local\Temp_autograph_generated_filehzbehghn.py", line 14, in tf__gradient_penalty_loss
gradients = ag
.converted_call(ag__.ld(compute_gradients), (ag_.ld(y_pred), [ag__.ld(averaged_samples)]), None, fscope)[0]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\priya\AppData\Local\Temp_autograph_generated_filerpgyuifg.py", line 11, in tf___compute_gradients
grads = ag
_.converted_call(ag__.ld(tf).gradients, (ag__.ld(tensor), ag__.ld(var_list)), None, fscope)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\keras_tensor.py", line 285, in array
raise TypeError(
TypeError: in user code:

File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\training.py", line 1401, in train_function  *
    return step_function(self, iterator)
File "c:\kush\BTP\f-CLSWGAN\LossFunctions.py", line 25, in gradient_penalty_loss  *
    gradients = _compute_gradients(y_pred, [averaged_samples])[0]
File "c:\kush\BTP\f-CLSWGAN\LossFunctions.py", line 14, in _compute_gradients  *
    grads = tf.gradients(tensor, var_list)
File "C:\kush\BTP\f-clswgan\clswgan\Lib\site-packages\keras\src\engine\keras_tensor.py", line 285, in __array__
    raise TypeError(

TypeError: You are passing KerasTensor(type_spec=TensorSpec(shape=(1024, 2048), dtype=tf.float32, name=None), name='random_weighted_average/add:0', description="created by layer 'random_weighted_average'"), an intermediate Kentermediate Keras symbolic input/output, to a TF API that does not allow registering custom dispatchers, such as `tf.cond`, `tf.function`, gradient tapes, or `tf.map_fn`. Keras Functional model construction only supalls that *do*ports TF API calls that *do* support dispatching, such as `tf.math.add` or `tf.reshape`. Other APIs cannot be called directly on symbolic Kerasinputs/outputs. You can work around this limitation by putting the opera `call` and cation in a custom Keras layer `call` and calling that layer on this symbolic input/output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.