LAION-AI / CLIP-based-NSFW-Detector

Other
299 stars 28 forks source link

torch version #7

Open rom1504 opened 1 year ago

rom1504 commented 1 year ago
class Normalization(nn.Module):
    def __init__(self, shape):
        super().__init__()
        self.register_buffer('mean', torch.zeros(shape))
        self.register_buffer('variance', torch.ones(shape))

    def forward(self, x):
        return (x - self.mean) / self.variance.sqrt()

class NSFWModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.norm = Normalization([768])
        self.linear_1 = nn.Linear(768, 64)
        self.linear_2 = nn.Linear(64, 512)
        self.linear_3 = nn.Linear(512, 256)
        self.linear_4 = nn.Linear(256, 1)
        self.act = nn.ReLU()
        self.act_out = nn.Sigmoid()

    def forward(self, x):
        x = self.norm(x)
        x = self.act(self.linear_1(x))
        x = self.act(self.linear_2(x))
        x = self.act(self.linear_3(x))
        x = self.act_out(self.linear_4(x))
        return x

clip_autokeras_binary_nsfw.zip

conversion notebook: port_nsfw_to_pytorch.zip

from @crowsonkb

SingL3 commented 1 year ago

Hi, @rom1504, I tried this and got this error: ValueError: Unknown optimizer: 'Custom>AdamWeightDecay'. Please ensure you are using akeras.utils.custom_object_scopeand that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

a574824551 commented 1 year ago

Hi, @rom1504, I tried this and got this error: ValueError: Unknown optimizer: 'Custom>AdamWeightDecay'. Please ensure you are using akeras.utils.custom_object_scopeand that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

model = tf.keras.models.load_model(path_to_model, compile=False)

doublex commented 1 year ago

Is it possible to inference using torch?

a574824551 commented 1 year ago

Is it possible to inference using torch?

yes, I have tried.