v9.1.1
Menu

Content Loss Computation

easyPRONeural Style TransferStyle TransferCV: Computational Photography

Compute the content loss between feature maps.

Content loss ensures the generated image preserves the high-level structure of the content image. Unlike style, we want to preserve spatial layout, so we directly compare feature activations:

Lcontent=12∑i,j(Fijgen−Fijcontent)2L_{content} = \frac{1}{2} \sum_{i,j} (F_{ij}^{gen} - F_{ij}^{content})^2

where:

  • FgenF^{gen} is the feature map of the generated image
  • FcontentF^{content} is the feature map of the content image

By matching features (not pixels), we preserve semantic content while allowing stylistic changes. Content loss is typically computed from a mid-level CNN layer that captures shapes and objects.

Example

Input
content_loss([[1, 2], [3, 4]], [[1, 2], [3, 4]])
Output
0.0
Reasoning
  • Comparing identical feature maps:

  • Differences: all zeros

  • Squared differences: all zeros

  • Sum: 0

  • Content loss: 0 / 2 = 0.0

  • Identical features mean identical content - zero loss.

Constraints

  • F_generated and F_content: feature maps with same shape
  • Return loss value rounded to 4 decimal places
  • Loss should be 0 when feature maps are identical

Learn this in

More from CV: Computational Photography

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.