CycleGAN implementation from scratch

Поделиться
HTML-код
  • Опубликовано: 17 янв 2025

Комментарии • 107

  • @AladdinPersson
    @AladdinPersson  3 года назад +13

    I'll be in the chat during the premiere to chat or if anyone has questions regarding the implementation! I've uploaded all the code to Github now with pretrained weights if you wanna evaluate the results: bit.ly/2OdZX75. If you have recommendations on GANs you think would make this into an even better resource for people wanting to learna bout GANs let me know in the comments below and I'll try to do it! Also a friendly remainder to please like the video if you want to see more of these from scratch videos :)
    I learned a lot and was inspired to make these GAN videos by the GAN specialization on coursera which I recommend. Below you'll find both affiliate and non-affiliate links, the pricing for you is the same but a small commission goes back to the channel if you buy it through the affiliate link.
    affiliate: bit.ly/2OECviQ
    non-affiliate: bit.ly/3bvr9qy
    Timestamps:
    0:00​ - Introduction
    0:32​ - CycleGAN summary
    7:45​ - Results paper vs our
    8:49​ - Outline for implementation
    9:25​ - Discriminator
    17:00​ - Generator
    28:15​ - Dataset
    38:00​ - Train function
    57:55​ - Ending

    • @sahil-7473
      @sahil-7473 3 года назад

      Hey bro.. just wondering. Whenever we start defining complex network (just now i inplemented ResNext), we start with the naive one and then we can optimize further later on. Is it alright it to do it? As for data Science concern, do they more focus on code optimization?

    • @datdat7919
      @datdat7919 3 года назад

      Aladdin Persson ! Can you make a video about StarGAN on scratch regarding multiple-domain style transfer? (a improved version of this). I highly appreciate it!

    • @guiwenliang6657
      @guiwenliang6657 Год назад

      It seems that you are urnning this implementation locally on your own PC with a reasonable GPU? Do you have a clip about how to setup your software environment to be able to run your code? I hope I can re-produce your implementation first, then I may be able to modify it a bit to do something a bit (or quite) different. Thanks a lot

  • @divymohanrai
    @divymohanrai 3 года назад +12

    One after another this playlist keeps getting better! Kudos this is hands down one of the best playlist on GAN's

    • @AladdinPersson
      @AladdinPersson  3 года назад +1

      Appreciate the kind words Divy, will work hard to always improve this playlist :)

  • @vishwak1998
    @vishwak1998 2 года назад +1

    This guy is the best. Giving out gold content out for free

  • @adesiph.d.journal461
    @adesiph.d.journal461 3 года назад +12

    Amazing Job! I am learning a lot from your videos and using them on a daily basis in my Research! Have a bunch of papers in submission will reach out to you and share incase they get accepted!

    • @tasnimmeem1158
      @tasnimmeem1158 4 месяца назад

      I am curious about your papers! what happened next?

  • @kirashi5878
    @kirashi5878 3 года назад +6

    Amazing!!! I am a fresh learner of coding actually... I wish I can code freely just like you do

    • @RicoChr
      @RicoChr 3 года назад +6

      Coding is a lot harder than he makes it look :) I think he implemented all of these before and is just rewriting them while looking at his previous implementation. Don't be discouraged.

    • @buddybot9329
      @buddybot9329 2 года назад

      You don't learn coding my friend. First learn the concepts and learn how to write it programmatically and learn typing.

  • @FortePiano
    @FortePiano 3 года назад

    Thanks! I have been watching your videos for a while and you have a great content! You deserve a lot more subs and views. Keep it up!

  • @Hirxn.vaghela
    @Hirxn.vaghela 2 года назад +6

    I used to same code and same dataset but when I try to see the saved images, I can just see a whole black image. Can anyone tell me what am I missing?

    • @Song-z3h
      @Song-z3h 10 месяцев назад +1

      I have the same question with you. Have you solved it? May you teach me how to fix it? Thank you very much!

    • @Song-z3h
      @Song-z3h 10 месяцев назад

      Maybe your computer is not capable of dealing with this task. I have met the same problem, but when I try on our lab's platform, it works well.

  • @srich-k
    @srich-k 3 года назад

    Ngl the official implementation of CycleGAN throws me an error everytime. I saw this implementation. Went through the complete video. Replicated this implementation and boi it works so well. Thanks a lot. Great Channel definitely deserves a sub and a like

  • @rezkirian2646
    @rezkirian2646 3 года назад +6

    Dear Mr Aladdin,
    Thank you so much for the video and our work is amazing. Please pardon me asking but I could not find the code to test the trained model. After training the CycleGAN model, how do we perform a real life test on another set of images. Hope to hear from you and please continue your work.
    Thank you

  • @changliu3367
    @changliu3367 3 года назад

    Howdy! Your videos are so awesome, which help me a lot and from which I've learned a lot. Thanks a lot.

  • @fredyorellana8214
    @fredyorellana8214 2 года назад

    Great Content!! Thank you so much!

  • @sagarparmar6715
    @sagarparmar6715 3 года назад +1

    This is really helpful, thanks

  • @JibbyDE
    @JibbyDE 3 года назад

    dude you are so awesome. thanx for the great content!

  • @atasayin9626
    @atasayin9626 3 года назад

    Great work, thank you so much

  • @naveedbhuiyan9855
    @naveedbhuiyan9855 2 года назад

    I was planning to learn cycleGANs for my thesis and your code literally saved my ass. Until I saw this video I honeslty had a really hard time understanding the codes. Thank you soo much for this video

  • @starlite5097
    @starlite5097 2 года назад

    Awesome job!

  • @IvanRaphelJaison
    @IvanRaphelJaison 5 месяцев назад

    27:10 can someone explain me why he didnt use sequential and instead chose to use module list when all that does is the same thing in the end?

  • @abbasnosrat8898
    @abbasnosrat8898 3 года назад +2

    A_img = self.A_images[index % self.A_len if self.length_dataset==self.B_len else index]
    B_img = self.B_images[index % self.B_len if self.length_dataset==self.A_len else index]
    i belive this would fix the issue you mentioned during the implementation of the dataset class

  • @iottseng
    @iottseng 2 года назад +2

    Hi Aladdin, I found out that in the same loop, if the image is from the end of that loop, it will become better. But why? shouldn't it become better because of the end of epoch.

  • @yujunguo9505
    @yujunguo9505 3 года назад

    Thanks so much for your video, please more from scratch video!

  • @yimingqu2403
    @yimingqu2403 3 года назад +3

    RUclips caption went mad at 17:05.

  • @leomessi9268
    @leomessi9268 3 года назад

    I like your video very much! I like the way you teach. I can lean a lot from your videos. Hope you could make videos about Faster RCNN! Best wishes!

  • @moranreznik
    @moranreznik 3 года назад

    Great vid. Q: why have a Sequential inside the Block, instead of the three layers directly?

  • @mioschoneberg2179
    @mioschoneberg2179 3 года назад

    Great work, thank you so much! This is really helpful! How to save the trained model in cyclegan?

  • @muhammadarham6442
    @muhammadarham6442 3 года назад

    Great work explaining the paper and then implementing it. I have one request. As you have gone through the YOLO implementation already. Can you kindly explain DeepSORT and its implementation with YOlO?

  • @bardub1
    @bardub1 3 года назад

    Great, thx a lot :)

  • @Archana-iv3cv
    @Archana-iv3cv Год назад

    sir,can you explain about the maths models and formulae that has been used in cyclegan and also how it is implemented in codes.

  • @cloudmom9313
    @cloudmom9313 3 года назад

    Hi Aladdin, thank you for this tutorial!
    I have a question on 5:30, it is written Identity Loss is "H->Z", but what I see in the implementation is "H->H". Am I missing something?

    • @AladdinPersson
      @AladdinPersson  3 года назад +1

      H->Z here is just the Generator that's supposed to convert horse to zebra, so it's an identity loss where if we already have the convert image then it shouldn't do anything to the original image. Perhaps it's a confusing detail but it seems like you get the idea

    • @cloudmom9313
      @cloudmom9313 3 года назад

      @@AladdinPersson oh, now I see, thank you!

  • @masakkali9996
    @masakkali9996 3 года назад

    At 1:24 two generators F and G right??

    • @Marva123
      @Marva123 2 года назад

      That is correct

  • @Sweety-hi9tc
    @Sweety-hi9tc 3 года назад +2

    How long time (how many epochs) it takes to train the cycleGAN? Is it ok if I have a lot of black pixels at the beginning of training?

    • @DuduHarar1
      @DuduHarar1 3 года назад

      Did you succeed eventually? In the paper, the writers say they used 100 epochs under constant learning rate and then linearly decayed it to 0 in the next 100 epochs.

    • @Song-z3h
      @Song-z3h 10 месяцев назад

      Excuse me, I have the same question with you. I directly download the code from github, and when I run it , horse_0 and zebra_0 are black pixels, the same to horse_200 and zebra_200. But in the video, I see these pictures are full of color and content since the begining. Whether do I ignore or forget something important? In pycharm it runs showing 18%|█▊ | 239/1334 [11:53

  • @Ytpremium-v9s
    @Ytpremium-v9s Месяц назад

    Where is the dataset?

  • @guiwenliang6657
    @guiwenliang6657 Год назад

    It seems that you are urnning this implementation locally on your own PC with a reasonable GPU? Do you have a clip about how to setup your software environment to be able to run your code? I hope I can re-produce your implementation first, then I may be able to modify it a bit to do something a bit (or quite) different. Thanks a lot

  • @u_luana.j
    @u_luana.j 3 года назад +1

    Thanks for this video. I really had hard time reproducing CycleGAN paper and came here. There are several questions though..
    1. For the fourth convolution layer in the discriminator(256->512 channels), I thought this was also stride=2 in paper.
    2. For the residual blocks, isn't the output should be ReLU(x+y)? I wanted to know if it meant something or was a mistake.
    3. In the training, you didn't implement learning rate decay nor image buffer. Did your model performed well without these?
    Thank you. I just spent a lot of time struggling with this and I didn't want to miss smallest reasons to my failure

    • @chinmayakaundanya3151
      @chinmayakaundanya3151 2 года назад

      Did u make all these changes and try training the model? Did u get better results?

  • @michaelsiagian3732
    @michaelsiagian3732 Год назад

    Hi there, just saw your video as well as the code from your github. I am new to coding so I am bit confused on the configuration part. For the train and val directory is it the pathway of the image from the dataset? As the TRAIN_DIR and VAL_DIR is used later in the training module of the python

  • @Ajan2-l9b
    @Ajan2-l9b Год назад

    at 55.56, after getting the result, how to find those picture in dataset, like from which picture it turned into horse to zebra and zebra to horse?

  • @hackercop
    @hackercop 3 года назад

    This was great thanks, I leanrn't a lot. I was thinking that at the end you could have made coded an eval.py that could evaluate the model on the trained weights instead of modifying code.

  • @eltonlobo8697
    @eltonlobo8697 3 года назад +1

    Hi,
    I have followed your implementation of CycleGAN just with no transformation other than resizing. I trained for 190 epochs and still couldn't get good results, what can I try to do to make the model perform better, do you think horizontal flip makes a significant difference?

    • @DuduHarar1
      @DuduHarar1 3 года назад

      Hi, just started the training. Saw that in the paper the writers say that they trained the network for 200 epochs (I think), with the first epochs with the constant learning rate and then another with linear decay.
      Did it work for you? What did you do eventually?

    • @eltonlobo8697
      @eltonlobo8697 3 года назад

      @@DuduHarar1 I didn't train it again since the time I can use GPU is limited. Maybe, this is a good point, I didn't read the paper, can you try using the same training approach like mentioned in the paper and check if you get good results if you can?

    • @DuduHarar1
      @DuduHarar1 3 года назад

      @@eltonlobo8697 yes sure, started this morning, hopefully my pc won’t die and finish this evening then I will be able to update about performance.

    • @DuduHarar1
      @DuduHarar1 3 года назад

      @@eltonlobo8697, ok so the network has been trained for two days, it didn't achieve results as in the paper nor as Alladin's, far from it. Maybe something wasn't right, maybe will try again next week..

    • @eltonlobo8697
      @eltonlobo8697 3 года назад

      @@DuduHarar1 What changes are you going to do when you train again?

  • @tihihoang
    @tihihoang 8 месяцев назад

    Can someone please help me on how to add summary() to the code? at 16:17, I tried to add summary(model, (5,3,256,256)) but got the error "Only 2D, 3D, 4D, 5D padding with non-constant padding are supported for now", then I tried summary(model, (3,256,256)) and got "Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same". I really want to figure out how a (5,3,256,256) matrix can be transformed into (5,1,30,30). Thank you in advance ^^

    • @tihihoang
      @tihihoang 8 месяцев назад

      problem solved, I used torchinfo instead of torchsummary

  • @soufianeaouane9476
    @soufianeaouane9476 3 года назад

    Hello, can you do a video about StyleGan!!

  • @osmanyasar9602
    @osmanyasar9602 3 года назад

    Why do you use nn.ModuleList in Generator class instead of nn.Sequential?

  • @adesiph.d.journal461
    @adesiph.d.journal461 3 года назад

    Hello Aladdin! A quick question. While computing the loss you compare the output of the discrimination with a matrix of 1's and 0's. Does the PatchGan output a Grid where each cell has 1 and 0?

    • @atharvachoudhary6974
      @atharvachoudhary6974 3 года назад +1

      the patchGAN model has a sigmoid activation at the end which make the output in the range (0,1). Not just 0s or 1s.

  • @ruizhou6956
    @ruizhou6956 3 года назад

    Hi, how to train it further for domain adaptation...since I can not change the cityscapes to dataset with binary masks
    expecting for that

  • @dhyaneswaran6449
    @dhyaneswaran6449 3 года назад

    From where can I get dataset?

  • @nouranfakhreldin2183
    @nouranfakhreldin2183 Год назад

    What is the difference between using nn.ModuleList(*[...]) and doing this: nn.Sequential([ .... ])?

  • @datdat7919
    @datdat7919 3 года назад +1

    Hello Guys, how can I train this without cuda and autocast? because there is only cpu in my computer. sorry I'm new to this CycleGan so really appreciate to get your help.

    • @atharvachoudhary6974
      @atharvachoudhary6974 3 года назад +1

      I believe you can replicate this code and it will still run. He has set up the config.DEVICE variable taking both conditions (if cuda is present or not) into account.
      I guess you'll just get a warning on the autocast line , won't get an error

    • @datdat7919
      @datdat7919 3 года назад

      @@atharvachoudhary6974 yes thank you for your help, I did resolve the problem by just removing the autocast!

    • @janabraun4775
      @janabraun4775 3 года назад

      @@datdat7919 hi how did you do that? I have the same problem that Cuda is not working on my laptop :)

    • @boquangdong
      @boquangdong 3 года назад

      nếu laptop của bạn ko có GPU. mình nói thiệt là bạn nên từ bỏ việc train trên lap nha. kể cả laptop có GPU thì cũng ko nên. model này thực sự nặng. mình lo cho cái laptop của bạn hơn. Bạn thử qua sử dụng google Colab nha.

    • @hafideel9orane743
      @hafideel9orane743 Год назад

      @@boquangdong please sir can u help me?

  • @TsaiBear1
    @TsaiBear1 Год назад

    Hi I tried your code by my dataset, but the generator seems like only can generated the same image as input. what should I do? thanks

  • @aymensekhri2133
    @aymensekhri2133 3 года назад +2

    I wrote the code like you exactly but when i run it. I got an error ( a GPU error ) is CoLab useful for training a cycleGAN ?
    Could you run your code on colab, please?
    If anyone train his own CycleGAN on colab and it's running please leave a comment?

  • @usamaahmad294
    @usamaahmad294 3 года назад

    Hello Sir I have question about cycleGAN that how we can fined tuned the cycleGAN for own data.

  • @bozhang9866
    @bozhang9866 3 года назад

    if you could teach Bayesian neural network, it will be greatly appreciated!

  • @korakoe6889
    @korakoe6889 3 года назад

    so like... how would you generate an image if you were to import this as a library in a script and send an image through a function

    • @korakoe6889
      @korakoe6889 3 года назад

      I ended up figuring this out, you should just be able to use the generation method used in the train function, but make sure you unsqueeze your tensors so that the tensor has a proper padding size

  • @janabraun4775
    @janabraun4775 3 года назад

    Hi :) thank you for the Tutorial! Can I somehow train my own models without using Cuda? I only habe a MacBook Pro with a 2,8 GHz Quad-Core Intel Core i7 processor.

    • @Mettisn221
      @Mettisn221 2 года назад

      You can train it on google colab

  • @Mesenqe
    @Mesenqe 3 года назад

    Nice and simple implementation. Guys, the least we can do is subscribe to this channel, please subscribe. One question I have with the implementation is that the residual block contains two Conv blocks (with ReLU and without ReLU). If you use this ResidualBlock in the generator class using the for loop, won't it generate 18 Conv blocks instead of 9? Because it will generate two Conv blocks in each loop.

  •  3 года назад

    Thanks for a great video. I was just wondering if I'm using py files, I guess I can change loading images from PIL to cv2, right? Because PIL is not supporting the py format. Or at least that is my guess why I'm getting this error: PIL.UnidentifiedImageError: cannot identify image file '/home/anya/Documents/ct/data/train/quarter_dose/1933.npy'

  • @alanjohnstone8766
    @alanjohnstone8766 3 года назад +3

    I do not think your dataset is correct. If getitem asks for index 6 as an example it will give the 6th horse picture AND the 6th zebra picture. In other words it compares the same horse with the same zebra every time. Surely you want a different match every time?
    You could shuffle the lists every epoch. I do not think the shuffle = True in the Dataloader has the desired effect as it just shuffles the order that indexes are requested.
    It may not matter too much if you have a largish batch size as the individual horse/zebra are not compared but if you had a batch size of one as in the paper you certainly would have a problem.

    • @hanzhouliu
      @hanzhouliu 2 года назад

      However, in the cycleGAN paper, the author set batch_size to 1. So we must use a different way to handle the shuffle instead of the dataloader built-in shuffle=True. Just like what you said, it always compared two same pictures.

  • @KarthiKeyan-ir5pk
    @KarthiKeyan-ir5pk 3 года назад

    what is padding mode ?

  • @Song-z3h
    @Song-z3h 10 месяцев назад

    Excuse me, why the images I generated are whole black pixels in each epoch? Whether something need to change since I just simply follow the video's code? I have been trapped in this question for 2 days but no way to solve. I am so upset.

    • @Song-z3h
      @Song-z3h 10 месяцев назад

      Sorry, my computer's gpu is not capable to carry this task, so the generated images are whole black. When I train it on our lab's platform, it works well!

  • @sandeeppadhi2826
    @sandeeppadhi2826 2 года назад +1

    I am getting following errors when I load a model with weights given in the github:
    raise RuntimeError('Error(s) in loading state_dict for {}:
    \t{}'.format(
    RuntimeError: Error(s) in loading state_dict for Generator:
    Missing key(s) in state_dict: "initial.0.weight", "initial.0.bias".
    Unexpected key(s) in state_dict: "initial.weight", "initial.bias".
    I am using python3.Can someone tell me how to solve this?Thankyou

    • @Marva123
      @Marva123 2 года назад

      In the file config.py, set the 'LOAD_MODEL = False; the error is telling you that the pretrained weights are not in the file. That is because you have run the model yet. This error will go away after the values has been saved.

  • @Georgesbarsukov
    @Georgesbarsukov 3 года назад

    Why not random sample a horse and a zebra instead of wrapping on dataset around itself?

  • @User-ri1jz
    @User-ri1jz 3 года назад

    This is definitely a stupid question, but I am gonna ask anyway.
    In train.py , there is a command "torch.cuda.amp.autocast() "
    I do not have a GPU, but will that command still run ?
    Thanks.

    • @Marva123
      @Marva123 2 года назад

      Yes, the program will still run it will just be run on the CPU instead and take 50x longer. In the file 'config.py' Aladdin even has a line of code on line 8, stating the DEVICE CPU or GPU it should run on: DEVICE = "cuda" if torch.cuda.is_available() else "cpu"

  • @usamaahmad294
    @usamaahmad294 3 года назад

    Because I doesn't have much amount of data.

  • @deoabhijit5935
    @deoabhijit5935 3 года назад

  • @FLLCI
    @FLLCI 3 года назад +3

    I was following the video fully focused and eating some snacks until I saw "disc_h(fake_hoes)" @44:35 second, I choked on my chips