Skip to content

Commit 48ebfa3

Browse files
authored
Merge pull request #1 from yunjey/master
更新代码
2 parents 4896cef + 57afe85 commit 48ebfa3

File tree

9 files changed

+37
-31
lines changed

9 files changed

+37
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ $ python main.py
4646

4747
## Dependencies
4848
* [Python 2.7 or 3.5+](https://www.continuum.io/downloads)
49-
* [PyTorch 0.4.0](http://pytorch.org/)
49+
* [PyTorch 0.4.0+](http://pytorch.org/)
5050

5151

5252

tutorials/01-basics/logistic_regression/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
# Hyper-parameters
8-
input_size = 784
8+
input_size = 28 * 28 # 784
99
num_classes = 10
1010
num_epochs = 5
1111
batch_size = 100
@@ -43,7 +43,7 @@
4343
for epoch in range(num_epochs):
4444
for i, (images, labels) in enumerate(train_loader):
4545
# Reshape images to (batch_size, input_size)
46-
images = images.reshape(-1, 28*28)
46+
images = images.reshape(-1, input_size)
4747

4848
# Forward pass
4949
outputs = model(images)
@@ -64,7 +64,7 @@
6464
correct = 0
6565
total = 0
6666
for images, labels in test_loader:
67-
images = images.reshape(-1, 28*28)
67+
images = images.reshape(-1, input_size)
6868
outputs = model(images)
6969
_, predicted = torch.max(outputs.data, 1)
7070
total += labels.size(0)

tutorials/01-basics/pytorch_basics/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898

9999

100100
# ================================================================== #
101-
# 4. Input pipline #
101+
# 4. Input pipeline #
102102
# ================================================================== #
103103

104104
# Download and construct CIFAR-10 dataset.
@@ -130,7 +130,7 @@
130130

131131

132132
# ================================================================== #
133-
# 5. Input pipline for custom dataset #
133+
# 5. Input pipeline for custom dataset #
134134
# ================================================================== #
135135

136136
# You should build your custom dataset as below.
@@ -186,4 +186,4 @@ def __len__(self):
186186

187187
# Save and load only the model parameters (recommended).
188188
torch.save(resnet.state_dict(), 'params.ckpt')
189-
resnet.load_state_dict(torch.load('params.ckpt'))
189+
resnet.load_state_dict(torch.load('params.ckpt'))

tutorials/02-intermediate/deep_residual_network/main.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# Hyper-parameters
1818
num_epochs = 80
19+
batch_size = 100
1920
learning_rate = 0.001
2021

2122
# Image preprocessing modules
@@ -37,11 +38,11 @@
3738

3839
# Data loader
3940
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
40-
batch_size=100,
41+
batch_size=batch_size,
4142
shuffle=True)
4243

4344
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
44-
batch_size=100,
45+
batch_size=batch_size,
4546
shuffle=False)
4647

4748
# 3x3 convolution

tutorials/02-intermediate/language_model/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def detach(states):
7676
loss = criterion(outputs, targets.reshape(-1))
7777

7878
# Backward and optimize
79-
model.zero_grad()
79+
optimizer.zero_grad()
8080
loss.backward()
8181
clip_grad_norm_(model.parameters(), 0.5)
8282
optimizer.step()

tutorials/02-intermediate/recurrent_neural_network/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def forward(self, x):
8585
.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
8686

8787
# Test the model
88+
model.eval()
8889
with torch.no_grad():
8990
correct = 0
9091
total = 0

tutorials/03-advanced/generative_adversarial_network/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
os.makedirs(sample_dir)
2323

2424
# Image processing
25+
# transform = transforms.Compose([
26+
# transforms.ToTensor(),
27+
# transforms.Normalize(mean=(0.5, 0.5, 0.5), # 3 for RGB channels
28+
# std=(0.5, 0.5, 0.5))])
2529
transform = transforms.Compose([
2630
transforms.ToTensor(),
27-
transforms.Normalize(mean=(0.5, 0.5, 0.5), # 3 for RGB channels
28-
std=(0.5, 0.5, 0.5))])
31+
transforms.Normalize(mean=[0.5], # 1 for greyscale channels
32+
std=[0.5])])
2933

3034
# MNIST dataset
3135
mnist = torchvision.datasets.MNIST(root='../../data/',

tutorials/03-advanced/image_captioning/README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,41 +16,41 @@ In the test phase, the encoder part is almost same as the training phase. The on
1616

1717
#### 1. Clone the repositories
1818
```bash
19-
$ git clone https://github.com/pdollar/coco.git
20-
$ cd coco/PythonAPI/
21-
$ make
22-
$ python setup.py build
23-
$ python setup.py install
24-
$ cd ../../
25-
$ git clone https://github.com/yunjey/pytorch-tutorial.git
26-
$ cd pytorch-tutorial/tutorials/03-advanced/image_captioning/
19+
git clone https://github.com/pdollar/coco.git
20+
cd coco/PythonAPI/
21+
make
22+
python setup.py build
23+
python setup.py install
24+
cd ../../
25+
git clone https://github.com/yunjey/pytorch-tutorial.git
26+
cd pytorch-tutorial/tutorials/03-advanced/image_captioning/
2727
```
2828

2929
#### 2. Download the dataset
3030

3131
```bash
32-
$ pip install -r requirements.txt
33-
$ chmod +x download.sh
34-
$ ./download.sh
32+
pip install -r requirements.txt
33+
chmod +x download.sh
34+
./download.sh
3535
```
3636

3737
#### 3. Preprocessing
3838

3939
```bash
40-
$ python build_vocab.py
41-
$ python resize.py
40+
python build_vocab.py
41+
python resize.py
4242
```
4343

4444
#### 4. Train the model
4545

4646
```bash
47-
$ python train.py
47+
python train.py
4848
```
4949

5050
#### 5. Test the model
5151

5252
```bash
53-
$ python sample.py --image='png/example.png'
53+
python sample.py --image='png/example.png'
5454
```
5555

5656
<br>

tutorials/03-advanced/image_captioning/sample.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
1515

1616
def load_image(image_path, transform=None):
17-
image = Image.open(image_path)
17+
image = Image.open(image_path).convert('RGB')
1818
image = image.resize([224, 224], Image.LANCZOS)
1919

2020
if transform is not None:
@@ -69,13 +69,13 @@ def main(args):
6969
if __name__ == '__main__':
7070
parser = argparse.ArgumentParser()
7171
parser.add_argument('--image', type=str, required=True, help='input image for generating caption')
72-
parser.add_argument('--encoder_path', type=str, default='models/encoder-2-1000.ckpt', help='path for trained encoder')
73-
parser.add_argument('--decoder_path', type=str, default='models/decoder-2-1000.ckpt', help='path for trained decoder')
72+
parser.add_argument('--encoder_path', type=str, default='models/encoder-5-3000.pkl', help='path for trained encoder')
73+
parser.add_argument('--decoder_path', type=str, default='models/decoder-5-3000.pkl', help='path for trained decoder')
7474
parser.add_argument('--vocab_path', type=str, default='data/vocab.pkl', help='path for vocabulary wrapper')
7575

7676
# Model parameters (should be same as paramters in train.py)
7777
parser.add_argument('--embed_size', type=int , default=256, help='dimension of word embedding vectors')
7878
parser.add_argument('--hidden_size', type=int , default=512, help='dimension of lstm hidden states')
7979
parser.add_argument('--num_layers', type=int , default=1, help='number of layers in lstm')
8080
args = parser.parse_args()
81-
main(args)
81+
main(args)

0 commit comments

Comments
 (0)